Home >Java >javaTutorial >Sharing some issues about multi-thread concurrency

Sharing some issues about multi-thread concurrency

零下一度
零下一度Original
2017-06-28 09:13:081524browse

Overview

1.volatile

Ensures that once the shared data is modified, it will be immediately synchronized to the shared memory (heap or method area).

2. The process of thread accessing data in the heap

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.

3. Instruction rearrangement

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.

Two common problems and solutions

1. Atomicity problem

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

The occurrence of visibility problem is related to the way threads access shared data. When a thread accesses a variable in the heap (method area), it first creates a

copy of the variable on the stack, and then synchronizes it to the heap after modification. If a thread has just created a copy, and another thread modifies the variable, which has not yet been synchronized to the heap,

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

has copied the data from the heap. Before the operation is completed, other threads modify the data. The modified data will not be synchronized to the current thread.

3. Ordering problem

In order to improve execution efficiency, the CPU will reorder instructions that have no dependencies. After reordering The execution result is the same as the sequential execution result.

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
Thread B:

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:

  1. Use the synchronization mechanism so that only one thread can access shared data at the same time, which is inefficient .

  2. 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn