Three elements of Java multi-threaded concurrent programming to ensure thread safety

When multiple threads want to share the value of an instance object, then the following three elements must be guaranteed when considering safe multi-threaded concurrent programming:

Atomicity (Synchronized, Lock)

Ordered (Volatile, Synchronized, Lock)

Visibility (Volatile, Synchronized, Lock)

Of course, since synchronized and Lock ensure that only one thread executes the synchronous code at each moment, it is thread-safe, and this function can also be implemented, but since the threads are executed synchronously, it will affect the efficiency.

The following is a detailed explanation of the 3 elements:

Atomicity : That is, an operation or multiple operations are either all executed and the process of execution is not interrupted by any factor, or none of them are executed.

 

In Java, the read and assignment operations of variables of primitive data types are atomic operations, that is, these operations are not interruptible, either executed or not.

 

Visibility : When multiple threads access the same variable, one thread modifies the value of the variable, and other threads can immediately see the modified value.

When a shared variable is modified by volatile, it guarantees that the modified value will be updated to main memory immediately, and when another thread needs to read the shared variable, it will go to memory to read the new value.

Ordinary shared variables cannot guarantee visibility, because after ordinary shared variables are modified, it is uncertain when they are written to main memory. When other threads read it, the memory may still have the old value at this time, so it cannot be Guaranteed visibility.

The steps of updating the main memory: the current thread sets the cache line of the cache variable in the working memory of other threads to be invalid, and then the current thread updates the value of the variable to the main memory, and updates the cache line of other threads to the new one after the update is successful. main memory address

 

When other threads read the variable and find that their cache line is invalid, it will wait for the main memory address corresponding to the cache line to be updated, and then go to the corresponding main memory to read the latest value.

 

Ordered : The order in which the program is executed is executed in the order of the code.

 

In the Java memory model, the compiler and processor are allowed to reorder instructions, but the reordering process will not affect the execution of single-threaded programs, but will affect the correctness of multi-threaded concurrent execution.

 

A certain "order" can be guaranteed by the volatile keyword.

 

 

 

 


When dealing with concurrent programming, as long as the program satisfies atomicity, visibility and ordering, the program will not have the problem of dirty data.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325796876&siteId=291194637