The concept of visibility
A thread changes the value of a shared variable to be seen by other threads in a timely manner
Shared variable Value: If a variable is used in the working memory of multiple threads and there is a copy, then this variable is a shared variable of these threads
Two important provisions:
- All operations of a thread on a shared variable must be made in its own working memory and cannot be read and written directly from main memory
- Variables in the working memory of other threads cannot be accessed directly between different threads, and inter-thread variables are worth passing through main memory to complete
JMM (Java Memory Model)
- All of the variables are stored in main memory
- Each thread has its own independent working memory, which holds a copy of the variable used by the thread (a copy of the variable in main memory)
Principle of realization of shared variable visibility
Thread 1 Flushes the updated shared variable in the working memory 1 to main memory, and the main memory updates the value of the latest shared variable to work memory 2, thread 2 can see the modification of thread 1 in time
Synchronize achieving visibility
- JMM Two articles on synchronize
The current value of the shared variable must be flushed to main memory before the line threads unlocked
Line Cheng is the value of the shared variable in the working memory will be emptied so that when using shared memory, the most recent value needs to be re-read from the main memory (lock and unlock required the same lock)
- Re-order As-if-serial
Volatile for visibility
- volatile keyword:
Can guarantee the visibility of volatile variables
The atomicity of volatile variable compound operations cannot be guaranteed
- How volatile can achieve memory visibility
By adding a memory barrier and disable reordering optimizations to achieve
When a write operation is performed on a variable, a store barrier command is added after the write operation
Volatile variable execution operation, a load barrier command is added before the read operation
- Volatile does not guarantee atomicity
When a-thread's working variable has read the value of a variable, even if the B thread changes the value of the variable and stores it in memory, the A-thread load is not triggered because the a thread reads the load.
- Atomicity of guaranteed variable self-increment operation
Using the Synchronize keyword
Using Reentrantlock (java.until.concurrent.locks)
Using Atomicinterger
(vava.util.concurrent.atomic)
- Volatile application occasions
At the same time meet:
- A write operation on a variable does not depend on its current value.
Not satisfied: number++,count=count*5, etc.
Meet: Boolean variable, record temperature change variables, etc.
- The variable is not included in the invariant with other variables
Not satisfied: Invariant type Low<up
Final can also guarantee memory visibility synchronize and volatile comparisons
Volatile does not require locks and does not block threads
Volatile reads are equivalent to locking from memory visibility, and volatile writes are equivalent to unlocking
Synchronize can guarantee both visibility and atomicity
Coding Personal Blog Link
Reference-Video
Multi-thread memory visibility