Java up.the volatitle multithreading issues

  • 2020-04-01 02:13:20
  • OfStack

As we know, all operations that set variable values in Java are atomic except for variables of type long and double, which means that there is no need to synchronize simple reads and writes to variable values.

Before JVM 1.2, Java's memory model implementation always read variables from main memory and did not require special attention. As the JVM matures and optimizes, the use of the volatile keyword in multithreaded environments is now important. Under the current Java memory model, threads can save variables in local memory (such as the machine's registers) rather than read and write directly from main memory. This can cause one thread to change the value of a variable in main memory, while another thread continues to use a copy of the value of the variable in the register, resulting in inconsistent data. To solve this problem, simply declare the variable as volatile (unstable), as we did in this program, which indicates to the JVM that the variable is unstable and is read from main memory each time it is used. The general said
In a multitasking environment, all flags Shared between tasks should be volatile.

Volatile modifies a member variable to force the member variable's value to be stressed from Shared memory each time it is accessed by a thread. Furthermore, when a member variable changes, the thread is forced to write the change back to Shared memory. This way, at any given time, two different threads will always see the same value of a member variable.

The Java language specification states that, for optimal speed, threads are allowed to keep private copies of Shared member variables and only compare the original value of the Shared member variable when the thread enters or leaves the synchronized block.

Therefore, when multiple threads interact with an object at the same time, it is necessary to pay attention to the thread to get timely changes in the Shared member variables.

The volatile keyword tells the VM that a private copy of a member variable cannot be kept for that member variable, but that it should interact directly with a Shared member variable.

Advice: use volatile on member variables accessed by two or more threads. Do not use when the variable to access is already in a synchronized code block, or is a constant.

Because the use of volatile masks the necessary code optimizations in the VM, it is inefficient, so use this keyword only when necessary.

In the implementation of virtual machine, int,char and other basic types are one word long. A long and a double are two words long. in Some of the In the implementation of a virtual machine, two words long may Will be operated as two atomic word lengths.

If you do not modify long and double with volatile, and if multiple threads access the variable, the overall nonatomic nature of the long operation results in confusion.
For example: int, one thread writes 4, another writes 5. It must end up being 4 or 5.


Related articles: