JAVA realize the principles of the lock mechanism (Starter Edition)

Concurrent programming lock can not be separated, but each time the mere mention of talks lock locking problems encountered, the following simplified description of the lock for implementation, facilitate easy to understand!

I. Introduction

In JAVA lock, a total of four states: no lock status, tend to lock status, lock status lightweight and heavyweight lock state (from low to high order, locked escalating competition)

JAVA locks can only upgrade but can not downgrade, the purpose is to improve the efficiency of obtaining and release locks.

Second, the introduction of the object head

In HopSpot virtual machine, the object is divided into three parts in the memory store: object header (Header), instance data (Instance Data) and alignment padding (Padding). For more information see Java object structure: the Java object structure

java object header contains three pieces of information, as follows:

Mark Word locked state stored in the object header to JDK Example 32:

Introduce three lock state

1, biased locking

In the absence of competition in a multi-threaded lock case, to make the same thread less costly to acquire a lock introduced a biased locking.

1.1, tend to lock acquisition process
  • (1) Access Mark Word identifies whether the biased locking set to 1, i.e., whether or not the lock flag is confirmed to be biased state 01--.
  • (2) If the state to be biased, then the test points to the current thread if the thread ID, and if so, proceeds to step (5), otherwise go to step (3).
  • (3) If the thread ID is not pointing to the current thread, the CAS operation lock by competition. If the competition is successful, Mark Word thread ID is set to the current thread ID, then execute (5); if competition fails to perform (4).
  • (4) If the CAS obtain biased locking fails, it means there is competition. Biased locking thread gets when reaching the global security point (safepoint) is suspended, biased locking upgraded to lightweight lock, then blocked at the point of thread-safe synchronization code continues down.
  • (5) perform synchronization code.

CAS is a lock-free algorithms, CAS has three operands, memory value V, the expected value of the old A, to be modified a new value B. If and only if the expected value of the A and V are the same memory value, the memory value V revised to B, or do nothing.

1.2, biased locking release

Use a biased locking mechanism until it releases the lock of competition there, so when another thread tries to lock biased competition, held biased locking thread will release the lock. Biased locking revocation, need to wait for global security points (without the byte code is executing at this point in time), it will pause the thread that owns the lock bias, the object is to determine whether the lock is locked state, return to the revocation biased locking unlocked (flag "01") or lightweight lock (flag "00") state.

2, lightweight lock

2.1, a lightweight lock lock

Thread prior to performing synchronized block, JVM will be created in the current thread's stack frame space for storing the locks, and the object head of Mark Word copied to the locks (Lock record), the official became Displaced Mark Word. Then try to head the object to point to lock Mark Word replacement recorded pointers CAS algorithm. If successful, the current thread to acquire the lock, and if that fails, represents another thread lock contention, the current thread will try to use to get the spin lock.

2.2, a lightweight lock unlocked

When the lightweight lock unlocked, it will use atomic CAS operation will replace the object head back to Displaced Mark Word, if successful, it means that there is no competition occurs. If it fails, there is competition represents the current, the lock will be expanded into a heavyweight lock.

Because the spin will consume CPU, in order to avoid unwanted spin (such as thread to acquire a lock to be blocked), once heavyweight lock to lock escalation, will not return to the lightweight lock state. When another thread tries to acquire the lock will be blocked to live, when the thread holding the lock release will wake up these threads, the thread will be awakened a new round of competition wins lock.

3, heavyweight lock

In multi-threaded programming synchronized been a veteran role, a lot of people call it the "heavyweight lock." synchronized is called a lock monitor (monitor) is achieved through an internal object, but the monitor lock is dependent on the nature of the underlying operating system Mutex Lock realized, switching between the operating system of the user from the need to thread Cause core state to state, the high cost of conversion requires a relatively long period of time between the state, which is synchronized low efficiency. Therefore, this depends on the operating system Mutex Lock to achieve lock, we call it "heavyweight lock."

After the JDK1.6, synchronized got all sorts of optimization, in some cases, it has not so heavy.

Fourth, the comparative advantages and disadvantages of lock

Note: This article comes from the main points of the "art of java concurrent programming" and a summary of some personal learning online article.

Guess you like

Origin juejin.im/post/5d0ce437f265da1b9253e321