Java memory Model (JMM): Describes the Vangen rules for various variables (thread-shared variables) in Java programs, as well as the underlying details of storing variables into memory and reading out variables from memory in the JVM. A shared variable means that a variable in one thread is also visible in another thread.
Implementation of shared variable visibility: Assume there are currently two threads: Thread 1 and Thread 2. Thread 1 reads the main memory of the x=0 into the working memory of thread 1 and performs the x=1 operation; The main memory updates the latest value of x, that is X=1, and the last thread 2 of the working memory updates the latest value of main memory.
How shared memory visibility is implemented: synchronized implementation and volatile implementation
1.synchronized implementations:
Before the ① line threads unlocked, the updated value of the shared variable must be flushed to the main memory;
② line Cheng, the value of the shared variable in the work is emptied, thus using shared variables requires the most recent value to be re-read from within the master ( Note: Locking requires the same lock as unlocking )
That is, changes to shared variables before line threads unlocked are visible to other threads the next time they are loaded. The process by which threads execute mutex code is as follows:
1?? Get mutual exclusion??
2?? Empty working memory
3?? Copy the most recent value from the main memory of the variable to the working memory
④ Code Execution
5?? Refreshes the updated shared variable values into main memory
6?? Release mutex??
PS: Reordering and As-if-serial
(1) reordering is the order in which code is written and the order in which implementation is implemented. Command reordering is a compiler or processor optimized to improve program performance
1?? Compiler-Optimized reordering (compiler Optimizations);
2?? Instruction level parallel reordering (processor optimization);
3?? Memory system reordering (processor optimizations)
(2) as-if-serial is anyway reordered, the result of the program execution code is the same as the sequential execution.
In a single thread, reordering does not affect the visibility of memory. However, in multi-threading, reordering may affect the visibility of memory. Specific as follows:
Cross-execution of ① threads--------------------------------------------------------------------------------------atomicity
② re-ordering--------------------------------------------------------------------------atomicity in conjunction with thread cross-execution
③ the shared variable is not updated-----------------------------------------------------------------visibility in the main memory immediately after the update
Synchronized Solution: Lock + atomicity
Volatile solution: Achieve visibility, but there is no guarantee of atomicity.
Volatile visibility implementation: memory barrier and no reorder optimization.
1?? When a write operation is performed on a volatile variable, a store barrier command is added after the write operation;
2?? When you perform a read operation on a volatile variable, a load barrier command is added after the read operation;
A volatile variable, when accessed by a thread, forces the value of the variable to be reread from the main memory, forcing the thread to update the current value to the main memory at any point in time, so that the variable's current value is always visible to different threads at any time.
Java Memory Model Learning notes