The use of static variables in Java multithreading

 

Reprinted from: http://blog.csdn.net/yy304935305/article/details/52456771

Sometimes, whether there is a conflict in the use of static variables in multithreading, whether there is a security problem is not very sure. In the process of using it, it is a bit vague, and I always want to find some time to investigate it, but it is always stranded due to the urgency of the development project time. I think it's my excuse to talk about the busyness of the project without doing further research!

    Mr. Lu Xun once said: "Time is like water in a sponge, as long as you want to squeeze it, there will always be." No matter what the swollen (how) said, this is still to be done. If it is pushed further back, the potential impact may be greater. This is always a hidden danger that cannot but be eliminated.

    It's not about thinking about the pain, but realizing the importance of things and starting to act...  

     The above is personal gossip, and it is not enough. Now, let's get into the technical field with a few words!

    Threads are an inescapable focus area in our project. When it comes to threads, you often hear the term thread safety. So what is thread safety? In layman's terms, there is no resource conflict when threads access. In fact, this is a somewhat difficult concept to define, and it is not a concept that is easy for people to understand. "A class that can be called by multiple threads is thread-safe" " Java Programming Concurrency in Practice".

    Let's talk about the security issues of static variables, instance variables, and local variables under multi-threading!

    (1) Static variables: thread-unsafe

   1. Static variables: Variables defined using the static keyword. Static can modify variables and methods, and there are also static static code blocks. Member variables and member methods modified by static are independent of any objects of the class. That is, it does not depend on a class-specific instance and is shared by all instances of the class . As long as the class is loaded, the Java virtual machine can locate them in the method area of ​​the runtime data area based on the class name. Therefore, a static object can be accessed before any of its objects are created, without referencing any objects.

    Static member variables and member methods modified with public are essentially global variables and global methods. When an object of its class is declared, a copy of the static variable is not generated, but all instances of the class share the same static variable.

    2. Scenarios for the use of static variables:

    (1) When values ​​are shared between objects

    (2) When it is convenient to access variables

    3. Precautions for the use of static methods:

    (1) Non-static variables cannot be used in static methods, that is, instance variables of the class to which they belong cannot be directly accessed;

    (2) Non-static methods cannot be called directly within static methods;

    (3) The this and super keywords cannot be used in static methods;

    4. Verify the thread safety of static variables:

    (1) From the figure of program execution, we can see that there is wrong data in the execution result, which proves that there is a resource conflict problem in static variables.

    (2) Program running result diagram: 

    wKioL1XIYiCz51UGABceSj2NUtI169.bmp

    5. Conclusion: Static variables, also known as class variables, belong to the class object and are located in the method area. They are shared by all objects and share a memory. Once the value is modified, other objects are visible to the modification, so the thread is not safe.

    (2) Instance variables: thread-unsafe when singleton, thread-safe when non-singleton

    1. Instance variables: Instance variables belong to the class object, that is to say, they are private to the object instance and are allocated in the heap of the virtual machine.

    2. Verify the thread safety of instance variables:

    (1) From the program screenshot, we can see that when it is a singleton mode, there will be resource conflicts, and when it is not a singleton mode, there will be no thread conflicts.

    (2) Program running result diagram:

    figure 1:

    wKioL1XIYl_RoatmABZU8hrY9Ck087.bmp

    figure 2:

    wKioL1XIYpzTOhE2ABZK5iG2N3I856.bmp

 3. Conclusion: The instance variable is private to the instance object, and there is only one instance object in the system. In a multi-threaded environment, if the value changes, other objects are visible, so the thread is not safe; if each thread is in a different Executed in the instance object,

Then the modification between the object and the object does not affect each other, so it is thread-safe.

    (3) Local variables: thread safety

     1. Local variables: variables defined inside a method.

     2. Verify the safety of local variables:

    (1) It can be seen from the program screenshot that there is no resource conflict problem for local variables under multi-threading

    (2) Program running result diagram:

    wKiom1XIYLvxqpwpABZORsRL6uY028.bmp

    3. Conclusion: When each thread executes, it will put local variables in the memory space of its own frame stack, and threads are not shared, so there is no thread safety problem.

    (4) Thread safety of static methods

     1. If static variables are not used in static methods, there is no thread safety problem;

          The variables in the static method will be newly created when each thread calls them, and a storage unit will not be shared, so there is no thread conflict problem.

    The above is a little research on static variables, instance variables and local variables in a multi-threaded environment, and it is only for reference when you need or forget.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325232848&siteId=291194637