Overview
Ensures that once the shared data is modified, it will be immediately synchronized to the shared memory (heap or method area).
The thread creates a copy of the variable in the stack, and after the modification is completed, the data is synchronized to in the pile.
In order to improve execution efficiency, the CPU will reorder instructions that have no dependencies. If you want to control reordering, you can use volatile to modify a variable. The instructions before and after the instruction containing the variable are ordered independently, and the instructions before and after cannot be cross-ordered.
The so-called atomicity, It means that an operation cannot be interrupted, that is, in a multi-threaded concurrent environment, once an operation is started, it will be executed within the same CPU time slice. If multiple operations of the same thread are executed on different CPU time slices, due to stagnation in the middle, some shared data may be modified by other threads during subsequent operations, and the modification is not synchronized. to the current thread, causing the data operated by the current thread to be inconsistent with the actual data. This data inconsistency problem caused by incoherent execution is called an atomicity problem. 2. Visibility problem
At this time, there will be a phenomenon where two threads operate the same variable in the same state, such as i=9 , the initial value of variable i is 9, and the operation of each thread is to decrease 1. Two threads A and B access variables at the same time. B executes i-1 first. Before synchronizing the result i=8 to the heap, thread A also executes i-1. At this time, the state of i=9 is executed Twice, thread safety issues occurred. Reasons for thread safety issues: Modifications of shared data by one thread cannot be immediately seen by other threads.
volatile provides a solution:
Once a thread modifies the shared data modified by volatile, the modification will be immediately synchronized to the heap In this way, when other data accesses shared data from the heap, they always get the latest value in multiple threads.
Defects of volatile:
volatile can only guarantee that when a thread obtains data from the heap, it obtains the latest value among all current threads. If a Thread
3. Ordering problem
For example, in the source code:
int i=0;
int y=1;
During execution, the CPU may first execute "int y=1;" and then "int i=0;". The execution result is the same as the sequential execution result.
Instruction reordering is safe in a single-threaded environment, but problems may occur in a multi-threaded environment. For example:
Thread A:
s=new String("sssss");//指令1flag=false;//指令2
while(flag){ doSome(); } s.toUpperCase();//指令3
If thread A executes sequentially, that is, executes instruction 1 and then executes instruction 2, there will be no problem with the execution of thread B. After the instructions are rearranged, if thread A executes instruction 2 first,
This is flag=true, switches to thread 2, terminates the loop, and executes instruction 3. Since the s object has not yet been created, a null pointer will appear. abnormal.
Reasons for the ordering problem:
One thread has sequence requirements for other threads' modification operations on shared data. For example, thread B requires thread A First execute instruction 1, and then execute instruction 2. Due to the rearrangement of instructions, the instructions are not actually executed in the required order. At this time, thread safety issues arise.
Solution:
Use the synchronization mechanism so that only one thread can access shared data at the same time, which is inefficient .
Using volatile, an instruction contains volatile-modified variables, then the execution order of this instruction remains unchanged, and the instructions before and after the instruction can be independent Rearrange, cross-rearrangement is not possible.
refer to:
The above is the detailed content of Sharing some issues about multi-thread concurrency. For more information, please follow other related articles on the PHP Chinese website!