Java concurrent programming summary 3: synchronized, volatile, java object structure, CAS

Refer to the blog I wrote before:

Java concurrent programming (three): synchronized, volatile detailed
java object structure

 

Sort out the overall structure:

1. Synchronized keyword (lock: blocking, heavyweight ) , object lock mechanism monitor;

2. CAS (lock: optimistic, non-blocking, lock-free ), and the three existing problems;

3. Java object header structure:

  • Contains: MarkWord , Klass Word, array length, object body, alignment word;
  • Klass Word: JVM uses this pointer to determine which class instance the object is .
  • The four lock states in MarkWord: no lock 01, bias lock 01, lightweight lock 00, heavyweight lock 10;
  • Biased lock: no CAS operation is required to lock and unlock;
  • Lightweight lock: Use CAS to replace the Mark Word in the object header with a pointer to the lock record . If it succeeds, the current thread acquires the lock. If it fails, it means that other threads compete for the lock, and the current thread tries to use spin to acquire the lock;
  • The lock upgrade process:
  1. When the initial lock object is just created, there is no thread to compete. The Mark Word of the object is the first situation in the figure below. This biases the lock flag to 0 and lock state 01, indicating that the object is in a lock-free state (no thread competition) it).
  2. When there is a thread competing for a lock, first use a biased lock to indicate that the lock object prefers this thread. This thread needs to execute any code associated with the lock without any further checks and switching. This kind of competition is not fierce ( To be precise: with only one thread and no competition), the efficiency is very high. At this time, Mark Word will record the ID of the thread he prefers and treat the thread as his acquaintance. The second situation is shown in the figure below.
  3. When two threads start to compete for this lock object, the situation changes, and it is no longer a biased (exclusive) lock. The lock will be upgraded to a lightweight lock. The two threads compete fairly. Which thread takes the lock object first and executes the code , The Mark Word of the lock object executes the lock record in the stack frame of which thread. The third situation is shown in the figure below.
  4. If the competing lock object has more threads, resulting in more switching and waiting, the JVM will upgrade the lock of the lock object to a heavyweight lock. This is called a synchronization lock. The mark word of the lock object changes again. Point to a monitor object, this monitor object uses the form of a collection to register and manage queued threads. The fourth situation is shown in the figure below.

4. Volatile keyword (lock: non-blocking, lightweight):

  • Involving JMM, happens-before, three major properties: atomic, visible, and consistent;
  • Implementation of volatile memory semantics: memory barrier (4 types);
  • Volatile write is to insert memory barriers at the front and back respectively , while volatile read operation is to insert two memory barriers at the back ;

 

Guess you like

Origin blog.csdn.net/ScorpC/article/details/113822708