Concurrent programming (1): volatile keyword

One: JAVA memory model

 

 

Knowledge points involved:

Multi-level cache, MESI protocol, cache coherence protocol, bus sniffing mechanism, atomic operation (8 atomic operations)

 

 

 The main function of the volatile keyword is to ensure the visibility of variables and then another role is to prevent instruction reordering. why?

 

Visibility: The MESI protocol (a cache coherence protocol that supports write-back strategy) and the bus sniffing mechanism determine that volatile can guarantee visibility;

Detailed explanation: The MESI protocol stipulates under what circumstances the data in the working memory (also called cache) is written back to the main memory, and the bus sniffing mechanism compares the variables in the working memory (cache) with the variables in the main memory. Combined, the MESI protocol stipulates that the working memory write operation will synchronously refresh the variable value of the main memory, and the bus sniffing mechanism determines whether the current variable value of the write operation is consistent with the main memory. If they are consistent, the write operation can be performed (Indicates that the value of the main memory variable has not been modified by other threads).

MESI protocol: Modified, Exclusive, Shared, Invalid; a cache coherency protocol that supports write-back strategy.

 

For details of MESI agreement, please see: https://www.cnblogs.com/yanlong300/p/8986041.html

Prevent instruction rearrangement: the reason is the memory barrier

Detailed explanation: memory barriers include Store Memory Barrier (write memory barrier), Load Memory Barrier (read memory barrier); the corresponding atomic operations are store and load;

 

 

lock: When unlocked, jvm will force the CPU cache to be refreshed, causing the current thread to change and be visible to other threads.

volatile: mark the volatile field, during the write operation, it will force the CPU cache to be refreshed, mark the volatile field, and read the memory directly every time it is read. (StoreLoad Barriers)

final: Just-in-time compiler will insert memory barrier after final write operation to prohibit reordering and ensure visibility

 

Two: three important characteristics of concurrent programming

 

  1. Atomicity: An operation or multiple operations, either all operations are executed and will not be interrupted by any factors, or all operations are executed or not executed. synchronized can guarantee the atomicity of code fragments.
  2. Visibility: When a variable modifies a shared variable, other threads can immediately see the latest value after modification. The volatile keyword can guarantee the visibility of shared variables.
  3. Ordering: The order of the code in the execution process, Java optimization in the compiler and runtime, the execution order of the code may not be the order when the code is written (instruction reordering). The volatile keyword can prohibit instructions from reordering optimization.

 

Three: The difference between the synchronized keyword and the volatile keyword

 

  • The volatile keyword is a lightweight implementation of thread synchronization, so volatile performance is definitely better than the synchronized keyword. But the volatile keyword can only be used for variables and the synchronized keyword can modify methods and code blocks. The synchronized keyword has been implemented after JavaSE1.6. It mainly includes biased locks and lightweight locks introduced to reduce the performance consumption caused by acquiring and releasing locks, and various other optimizations. The execution efficiency has been significantly improved, and actual development is in progress. There are more scenarios where the synchronized keyword is used.
  • Multi-threaded access to the volatile keyword will not block, while the synchronized keyword may block
  • The volatile keyword can guarantee the visibility of data, but cannot guarantee the atomicity of the data. Both synchronized keywords are guaranteed.
  • The volatile keyword is mainly used to solve the visibility of variables between multiple threads, while the synchronized keyword solves the synchronization of accessing resources between multiple threads.

Published 27 original articles · praised 0 · visits 9932

Guess you like

Origin blog.csdn.net/weixin_38246518/article/details/105543158