1. Jmm:java memory model (Java RAM models)
Two articles on synchronized :
1. Before line threads unlocked, the latest value of shared variable must be flushed to main memory
2. When line Cheng, the value of shared variables in working memory will be emptied, so the most recent values need to be reread from main memory when using shared variables (note: Lock and unlock required to be the same lock)
Note: Changes to shared variables before line threads unlocked are visible to other threads the next time the lock is added
2. The process by which the thread executes the mutex code:
1. Get the Mutex lock
2. Empty working memory
3. Copy the latest copy of the variable from the main memory to the working memory
4. Execute code
5. Refresh the value of the changed shared variable to main memory
6, release the mutex lock
noun concept :
1, reorder : The Order of code writing is different from the actual order of implementation; command reordering is an optimization that the compiler or processor makes to improve program performance.
1. Compiler-Optimized reordering (compiler Optimizations)
2, instruction-level parallel reordering (processor optimization)
3. Reordering of memory systems (processor optimization)
2,as-if-serial: Regardless of reordering, the results of the program execution should be consistent with the results of the code sequence execution (Java compiler, runtime and processor will ensure that Java in a single thread to follow the as-if-serial semantics)
Note:
1. Reordering does not cause memory visibility issues for single threads
2. Reordering may cause memory visibility issues when programs are interleaved in multiple threads
Visibility Analysis :
Causes of shared variables not being visible between threads:
1. Cross-execution of threads
2. Reordering with thread crossover execution
3, the shared variable updated value is not in the working memory and the main memory in time to update
-------------------------------------------------------------------------------------------------------------
Volatile for visibility
1. Volatile keyword:
1, to ensure the visibility of volatile variables
2. The atomicity of volatile variable compound operation cannot be guaranteed
2. How volatile can achieve memory visibility:
In-depth: implemented by adding memory barriers and preventing reordering optimizations.
1. When writing to volatile variables, a store barrier command is added after the write operation
2. When performing a read operation on a volatile variable, a load barrier command is added before the read operation
In layman's terms: a volatile variable forces the value of the variable to be reread from main memory each time it is accessed by a thread, and when that variable changes, it forces the thread to flush the most recent value to the main memory. At any point in time, different threads can always see the latest value of the variable.
3, the thread writes the volatile variable process:
1. Change the value of the volatile variable copy in the thread's working memory
2. Flushes the changed copy value from the working memory to the main memory
The process by which threads read volatile variables:
1. Read the latest value of the volatile variable from main memory into the working memory of the thread
2. Read a copy of the volatile variable from the working memory
4. To use volatile variables safely in multiple threads, you must also satisfy:
1, the write operation to the variable does not depend on its current value
2. The variable is not included in the invariant with other variables
5. Ensure the atomicity of number self-increment operation:
1. Use synchronized keyword
2, the use of Reentrantlock (Java.util.concurrent.locks package)
3, the use of Atomicinteger (Java.util.concurrent.atomic package)
--------------------------------------------------------------------------------------------------------------- -------
6, synchronized and volatile comparison
1, volatile does not need to lock, more lightweight than synchronized, will not block the thread
2, from the memory visibility point of view, volatile reading equivalent to lock, volatile write equivalent to unlock
3, synchronized both to ensure visibility, but also to ensure atomicity, and volatile can only guarantee visibility, can not guarantee atomicity
Java multi-thread memory visibility-synchronized, volatile