What does the volatile keyword mean in Java

In java thread concurrency processing, there is a keyword volatile. At present, there is great confusion about the use of this keyword. It is thought that everything can be done when multithreading concurrency processing is carried out.

Java language supports multithreading. In order to solve the problem of thread concurrency, synchronous block and volatile keyword mechanism are introduced into the language.

synchronized

Synchronization blocks are familiar to everyone. They are implemented through the synchronized keyword. With synchronized and block statements, only one thread can be used at the same time during multi-threaded access

Synchronized decorated method or code block.

volatile

For variables modified with volatile, the thread will read the most modified value of the variable every time it uses the variable. Volatile is easily misused for atomic operations.

Let's take a look at an example. We implement a counter. Each time the thread starts, we will call the counter Inc method to add one to the counter

Execution environment - JDK version: jdk1.0 6.0_ 31. Memory: 3G CPU: x86 2.4G

Running result: counter count=995

The actual operation result may be different every time. The local result is: running result: counter Count = 995. It can be seen that in a multithreaded environment, counter Count did not expect the result to be 1000

Many people think that this is a multi-threaded concurrency problem. We can avoid this problem by adding volatile before the variable count. Let's modify the code to see if the result meets our expectations

Running result: counter count=992

The running result is still not 1000 as we expected. Let's analyze the reasons

In the article "Java garbage collection and sorting", the memory allocation at JVM runtime is described. One memory area is the JVM virtual machine stack, and each thread runs with a thread stack,

The thread stack stores the variable value information of thread runtime. When a thread accesses an object, it first finds the value of the variable corresponding to the heap memory through the object reference, and then puts the heap memory

Load the specific value of the variable into the thread's local memory and create a copy of the variable. After that, the thread will no longer have any relationship with the variable value of the object in the heap memory, but directly modify the value of the copy variable,

At a certain time after the modification (before the thread exits), the value of the thread variable copy is automatically written back to the variable of the object in the heap. In this way, the value of the object in the heap changes. The following figure

Read and load copy variables from main memory to current working memory use and assign execute code, change shared variable values store and write, and refresh relevant contents of main memory with working memory data

Use and assign can appear multiple times

However, these operations are not atomic, that is, after read load, if the count variable in the main memory is modified, the value in the thread working memory will not change because it has been loaded, so the calculated result will be different from that expected

For volatile decorated variables, the JVM virtual machine just ensures that the value loaded from main memory to thread working memory is up to date

For example, if thread 1 and thread 2 find that the value of count in main memory is 5 during read and load operations, the latest value will be loaded

After the thread 1 heap count is modified, it will be written to the main memory, and the count variable in the main memory will change to 6

Thread 2 has already performed read and load operations. After the operation, the variable value of main memory count will also be updated to 6

As a result, the two threads will still be concurrent after being modified in time with the volatile keyword.

The above is the meaning of volatile keyword in Java introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>