• 周日. 11月 27th, 2022

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

In depth analysis of Java Concurrent Programming Series

[db:作者]

1月 6, 2022

Premise undertaking

For the previous two articles on synchronized The article , It is mainly about synchronized In bytecode implementation and form of expression ( Synchronization code block and synchronization method )Java Concurrent programming series of in-depth analysis synchronized( The basic chapter ) As well as the implementation principle of first closing pin for synchronous lock, such as ObjectMonitor Data structure, waiting queue, synchronization queue, etc Java Concurrent programming series of in-depth analysis synchronized( Advanced ), This paper mainly introduces the structure of object lock in execution angle ( The status of the tag field ) And the relevant lock process related process carding, as well as the summary of the previous two chapters .

synchronized keyword

synchronized The main advantage of the lock mechanism is Java Language built-in locking mechanism , therefore ,JVM Free to optimize without affecting existing code .

Any object has a data structure called object header to support locking , But for larger objects, the overhead is higher .

java Each object in contains at least 2 A word (2 * 4 Bytes for 32bits & 2 * 8 Bytes for 64bits, Excluding compressed objects ).

  • The first word is called Mark Word. This is the head of an object , It contains different information , Including information about locks (32bitOS->8bit size 、64bitOS->16bit size ).

  • The second word points to metadata class The pointer to ,metadata class The word defines the type of the object . This part also includes VMT(Virtual Method Table).

Mark Word The structure of is as follows :

Mark Word According to the lowest two (Tag) The object state represented by ( Contains the type of lock ), Encoded different information .

If the object is not used as a lock ,Mark Word Recorded hashcode And the age of the subject (for GC/survivors).

besides , Yes 3 Each state corresponds to a lock : Lightweight lock , Heavyweight locks and deflection locks .

Lightweight lock

modern JVM Lightweight locks have been introduced

Avoid associating each object with the mutex/condition Variable ( Heavyweight lock

  • When there is no lock competition , Use atomic operations to enter and exit synchronization blocks

  • If lock contention occurs , Go back to the heavyweight lock of the operating system

  • Introducing lightweight locks will provide lock efficiency , Because most locks don’t compete .

The process of locking lightweight locks

When an object is locked ,mark word Is copied to the thread stack of the thread currently trying to acquire the lock (Execution stack) Lock record space for (lock record), Copied mark word Officially called displaced mark.

Use CAS Operation to try to make mark word Points to the lock record space of the current thread ( That is to say mark word Use in Pointer to the current thread lock record space ——stack pointer).

  • If CAS Successful operation , Then the thread gets the lock .

  • If CAS operation failed , There is lock competition , Then lock expansion occurs , Back to heavyweight lock .

  • The lock record space records the objects locked by the current execution method ( Find the lock object of the thread by traversing the thread stack ).

Lightweight lock before locking :

After the lightweight lock is locked :

The unlocking process of the quantum lock :

  • Unlock to use CAS Come and take displaced mark Write back the object’s mark word in .

  • If CAS Failure , Indicates that lock contention has occurred : Then the lock expands .( Notify other waiting threads that the lock has been released ) Set the lock record space to 0;

  • If lock inflation occurs , Then use 0 Replace displaced mark, If there is no competition , be CAS Set the lock record space to 0 after , stop it CAS operation .

Biased locking

The introduction of biased lock

On multiprocessors CAS Operations can be expensive .

  • Not only is there no competition for most locks , And it’s often used by the same thread .

  • Make a single thread get lock less overhead .

  • The cost is to make another thread get lock more expensive .

Bias lock locking process

  • The first time a lock object is acquired by a thread ,VM Set the flag bit in the object header to 101, I.e. bias mode .

  • Use at the same time CAS The thread that gets the lock ID Record in object mark word in , If CAS success , Then, each time the thread holding the biased lock performs the synchronization block related to the lock , No more task synchronization , Just compare Mark word Thread in ID Whether it is the current thread’s ID.

The unlocking process of the deflection lock

  • When another thread tries to acquire the lock , Bias mode ends . Depending on whether the lock object is currently locked , Return to unlocked or lightweight locked state after undoing bias .

  • VM Will stop the thread holding the biased lock ( actually ,VM Cannot stop a single thread , It’s an operation at a safe point ).

  • Traversing the stack of threads holding biased locks , Find the lock record space , take displaced mark Write to the oldest lock record space , Other writing 0.

Update the mark word. If it’s locked , Point to the oldest lock record space , otherwise , Fill in the unlocked value .

The characteristics of biased lock :

  • Prefer the first thread to get the lock :

stay mark word Of Tag Add one to the list

001 No lock state 
101 The state of being biased or deflectable (thread ID ==0 == unlock)

adopt CAS To get the deflection lock

  • For the thread holding the lock, the cost of lock acquisition and release is very small ( Just judge , Unwanted CAS Synchronous operation ).

If another thread locks the biased lock object , The lock retracts , Upgrade to lightweight lock ( Increased the cost of another thread getting lock ).

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注