Common problems based on Multithreading concurrency (detailed explanation)
I. overview
1.volatile
Ensure that once the shared data is modified, it will be synchronized to the shared memory (heap or method area) immediately.
2. The process by which a thread accesses data in the heap
The thread creates a copy of the data in the stack and synchronizes the data to the heap after modification.
3. Instruction rearrangement
In order to improve the execution efficiency, the CPU will reorder the instructions without dependencies. If you want to control reordering, you can use volatile to modify a variable. The instructions before and after the instructions containing the variable are sorted independently, and the instructions before and after the instructions cannot be cross sorted.
II. Common problems and Countermeasures
1. Atomicity
Atomicity means that an operation cannot be interrupted, that is, in a multithreaded concurrent environment, once an operation starts, it will be executed in 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, a shared data may be modified by other threads during the execution of subsequent operations, and the modification is not synchronized to the current thread, resulting in the data operated by the current thread inconsistent with the actual situation, This data inconsistency problem caused by inconsistent execution is called atomicity problem.
2. Visibility issues
The visibility problem is related to the way threads access shared data. Thread access heap For variables in (method area), first create a copy of the variable in the stack, modify it, and then synchronize it to the heap. If a thread just creates a copy, then another thread modifies the variable and has not synchronized it to the heap, then two threads will operate the same state of the same variable, such as I = 9, the initial value of variable I is 9, and the operation of each thread is Minus 1. Two threads a and B access variables at the same time. B executes I-1 first. Thread a also executes I-1 before synchronizing the result I = 8 to the heap. At this time, the state of I = 9 is executed twice, resulting in thread safety problems.
Cause of thread safety problem: the modification 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 synchronized to the heap immediately, so that other data will always obtain the latest value in multiple threads when accessing the shared data from the heap.
Defects of volatile:
Volatile can only ensure that when a thread obtains data from the heap, it obtains the latest value of all current threads. If a thread has copied data from the heap, other threads modify the data before the operation is completed, and the modified data will not be synchronized to the current thread.
3. Order
In order to improve the execution efficiency, the CPU will reorder the instructions that have no dependencies, and the reordered execution results are the same as the sequential execution results.
For example, in the source code:
The CPU may execute "int y = 1;" first, Then execute "int i = 0;", The execution result is the same as the sequential execution result.
Instruction rearrangement is safe in a single threaded environment, and problems may occur in a multi-threaded environment. For example:
Thread a:
Instruction 2 thread B:
s.toUpperCase();// Instruction 3 if thread a executes in sequence, that is, execute instruction 1 and then execute instruction 2, there will be no problem with the execution of thread B. After instruction rearrangement, if thread a executes instruction 2 first, then flag = true, switch to thread 2, terminate the loop, and execute instruction 3. Since the s object has not been created, a null pointer exception will occur.
Causes of order problems:
A thread has sequential requirements for the modification of shared data by other threads. For example, thread B requires thread a to execute instruction 1 and then instruction 2. Due to the instruction rearrangement, it is not actually executed in the required order. At this time, there is a thread safety problem.
Solution:
1. Using the synchronization mechanism, only one thread can access the shared data at the same time, which is inefficient.
2. Using volatile, if an instruction contains variables modified by volatile, the execution order of the instruction remains unchanged, and the instructions before and after the instruction can be rearranged independently without cross rearrangement.
The above common problem based on Multithreading concurrency (detailed explanation) is all the content shared by Xiaobian. I hope it can give you a reference and support programming tips.