This is the 9th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
The scene is introduced into
For example, if two threads need to read the value of data, each thread loads a copy of data into its working memory, and each thread can read data=0. This way, while threaded code is running, copies of variables can be loaded directly from the local cache, without loading values from main memory, which improves performance.
Suppose thread A changes the value of the data variable to 1 and then writes the change to its local working memory. At this point, thread A’s working memory data value is 1, whereas the main memory and thread B’s memory data value is 0. As A result, thread A and thread B both operate on the same variable data, but when thread A changes the value of the data variable, thread B does not see it, and always sees an old copy of the value in its local working memory. \
This is the so-called visibility problem in Java concurrent programming: When multiple threads concurrently read and write to a shared variable, it is possible for one thread to change the value of the variable, but other threads cannot see it, that is, it is not visible to other threads.
What volatile does and how
The visibility problem can be solved simply by defining the data variable as volatile.
If thread A changes the value of the data variable, it will force the latest value of the data variable to be flushed back to main memory after modifying the data variable in its local working memory. The data variable value in main memory must be immediately changed to the latest value.
Second, if there is a local cache of the data variable in the working memory of another thread at this time, that is, a copy of the variable, it will force the data variable cache in the working memory of other threads to directly expire, and is not allowed to read and use it again.
Third, if thread B tries to read the value of the data variable from the local working memory while the code is running, it will find that the data has expired. At this point, the latest value of the data variable must be reloaded from main memory. \
A volatile variable is forcibly flushed back to main memory as soon as a thread changes its value. It then forces the expiration of the cache in the local working memory of another thread, and finally forces the reloading of the latest value from main memory when the variable value is read by another thread. This ensures that any changes made by one thread are immediately visible to other threads.
conclusion
Valatile’s main role is to ensure visibility and order. Orderliness involves complex instruction rearrangements, memory barriers, and so on, but volatile does not guarantee atomicity. If multiple threads modify the value of a variable at the same time, it is still possible to have the safety problem of multi-threading concurrency, resulting in the modification of data value disorder, atomic problems have to rely on synchronized, reentrantLock and other locking mechanisms to solve.