The bottom synchronized "Java concurrent programming art" of the realization of the principle

When the optimization learning lock, object header (Mark Word) is an essential part, because synchronized with locks is present object header inside. 32-bit virtual machine object header accounts for 64-bit (8 byte), the 64-bit virtual machine account object header 128 bits (16 bytes) [^ objectHead]; different types of layout object header is not the same :

  • Array types: Mark Word, Class Metadata Address, Array Length
  • Common types: Mark Word, Class Metadata Address

Mark Word representing an object HashCode or lock information
Class Metadata Address data indicating the type of the object corresponding to the address area in the process
Array Length indicates the length of the array (only in the presence of only the case where the object is an array)

The default object header should be represented as follows

Lock status 25bit 4bit 1bit whether it is biased locking 2bit lock flag
Stateless lock hashcode object Object generational Age 0 01

Specific Object Memory layout see this article

According to the JVM setting 1 , the specific allocation of time and will be different, as shown below

Process biased to lock the lock by weight

When you close the bias lock set, then the process will go left; otherwise take the process right.

Biased locking

Because in most cases, most are not locked in a multi-threaded race condition, and always get the same thread, so after adding 1.6 JVM biased locking and lightweight lock , and now a total of four kinds of lock status: Stateless lock , biased locking , lightweight lock , by weight of the lock . With the enhancement of competition in the thread, the lock will gradually upgrade (not downgrade).
Biased locking in without competition can improve synchronization performance in this area is mainly reflected in the biased locking only once CAS and lightweight lock requires two. It is a need to weigh the options, it's not in any case the program beneficial. If a lot of competition, then the revocation biased locking process will become a performance bottleneck.

When biased lock is available, the initialization of the object as shown dispensing head

Lock status 23bit 2bit 4bit 1bit whether it is biased locking 2bit lock flag
Biased locking Thread ID epoch Object generational Age 1 01

Locking procedure

  1. When the object header isBiased is 1 and the lock state is 01 , the biased lock is available, flow continues back to
  2. Determining whether the target object header comprising the thread ID, and if not, the object ahead is written directly to CAS this thread ID. At this point the lock is over

Lock revoked

Due to the use of a biased locking until the competition will take place the release mechanism, so when other competing biased locking thread, the thread will hold biased lock to release the lock.

  1. Waiting for the original thread holding the lock bias (hereinafter referred to as the original thread) runs to global security points (safe point)
  2. Pause original thread
  3. Check the original thread thread state, if out of the sync block, the weight bias; conversely upgraded lightweight lock
  4. Restore the original thread

Lightweight lock

Locking procedure

Note: Lightweight lock will remain, Wake happen when a lightweight lock unlocked because lock when the operation has been successfully CAS; CAS failure and thread will lock inflation immediately and block waiting wake.
Java concurrent programming reference pictures of art

  1. The first to enter the synchronized block, opened called Lock Record space for storing lock records
  2. The subject header of Mark Word copied to the current thread stack
  3. Try using CAS Mark Word replacement is pointing Lock Record pointer
  4. The third step is the successful operation, then Mark Word is set to 00 status, identity lightweight lock
  5. And then synchronize body
  6. The third part of the operation fails, get into the spin lock
  7. Spin lock acquisition failures reaches a threshold, expansion of the lock, to modify heavyweight lock (read state 10 )
  8. Thread blocks

Lock release process

  1. CAS will attempt Lock Record of Owner copied back Mark Word
  2. If the CAS operation is successful, it means that there is no competition occurs; otherwise see step 3
  3. Release the lock and wake up waiting threads

to sum up

This chapter is synchronized processes various grades and upgrade in the JVM were to explain, which is mainly controlled by the object head to control the level of some of the state of the lock. Biased locking by marking Thread ID to indicate that the current object has been occupied by a corresponding thread; lightweight lock is replaced Mark Word as Lock Record address to represent the current thread holds the corresponding object. No matter what kind of lock, there are different needs in different scenarios, you can refer to the following table to make a choice

Biased locking:

  • Advantages: No additional locking and unlocking consumption, and perform asynchronous method compared to the gap there is only a nanosecond
  • Disadvantages: If there is competition between the threads, will bring additional costs (biased locking revocation)
  • Applicable scene: suitable for only one thread synchronization block access to scenes

Lightweight lock:

  • Pros: competing threads will not cause congestion, and improving the response speed of the procedure
  • Cons: If still unable to get a lock, using spin will consume CPU
  • Applicable scene: the pursuit of appropriate practice, very fast sync block execution

Weight lock:

  • Pros: do not use managed competition thread, do not consume CPU
  • Disadvantages: thread is blocked, slow response time
  • Application scenarios: pursuit of certain sync block execution slower

This is a total lock on the revocation process, the expansion of the Internet to find other operations


  1. About biased locking of the relevant JVM settings: -XXBiasedLockingStartupDelay = 0 represents the start the program after a few seconds to activate biased locking -XXUseBiasedLocking = false means closed biased locking (It can be set to determine when competition would occur)

Guess you like

Origin www.cnblogs.com/codeleven/p/10963092.html