We don't just want to prevent a thread from using a certain state, another thread is modifying it, and we want the other thread to be able to see the change in state after a thread has modified a state.
First, visibility
Reorder: In the absence of synchronization, the compiler, the processor may make some adjustments to the execution order of the Code
For example, the following code, because the synchronization mechanism is not used, the read thread may not see the ready modification, and continue to loop, or because of reordering, see Ready's modified number still not modified and output 0
1. Failure data
A situation in which the wrong result is produced in a program that lacks synchronization. resulting in procedural uncertainties .
2, non-atomic 64-bit operation
Even the failure data is the data generated by the program's past operations.
But performing a non-atomic 64-bit operation, the JVM is decomposed into two 32-bit operations, which can result in incorrect values. Use the volatile keyword to resolve
3. Locking and visibility
The effect of locking is not limited to mutual exclusion, but also includes memory visibility.
A lock ensures that a thread can see the execution results of another thread in a predictable way. That is, the thread is visible after the first-thread result.
4. Volatile variables
A more lightweight synchronization mechanism than synchronized, the compiler and the runtime will notice that the variable is shared, and that the operation on that variable and other memory operations will not be reordered together .
The cost of reading volatile variables is minimal.
The best time in the development phase is Server mode, because the server schema will do more optimizations, such as reordering, which can cause concurrency problems
The locking mechanism guarantees visibility and atomicity, and volatile can only guarantee visibility and conditions of use:
- Write does not depend on current value
- No lock on Access
Ii. release and Escape
Publish: Enables an object to be used in code that is currently in effect, for example:
Escaping: An object that should not be published is published
Do not overflow the reference to this in the construction method and publish an incomplete object
Third, thread closure
When accessing shared mutable data, synchronization is often required. One way to avoid using synchronization is to not share data. If data is accessed only in single-wire range, no synchronization is required. This technique is called thread closure
Be sure that objects in the enclosing thread do not overflow
1, Ad-hoc thread closure: Completely program to implement thread closure, less use
2. Stack closure: object can only be accessed through local variables, because other threads cannot access the stack area of the execution thread
3, Theadlocal class threadlocal<t>, Map<thread, t>
The Threadlocal class is used to provide local variables inside the thread. This variable is accessed in a multithreaded environment (accessed through a Get or set method) to ensure that the variables of each line thread are relatively independent of variables within other threads.
See: (EXT) http://qifuguang.me/2015/09/02/[java%e5%b9%b6%e5%8f%91%e5%8c%85%e5%ad%a6%e4%b9%a0%e4%b8%83]%e8%a7%a3%e5% af%86threadlocal/
Chapter III: Sharing of objects--java concurrent programming combat