[Questions from the brush surface to the underlying Java Construction of Knowledge System] -2 biased locking piece locks -synchronized

By building on a pyramid structure, from different angles, progressive approach to the synchronized keyword made the introduction,

Quick Jump: https://www.cnblogs.com/xyang/p/11631866.html

This article from the various "components" of the underlying implementation to proceed, detailed dismantling it works.

This article will be divided into the following sections 4 Content:

  Section I: Introduction MarkWord and LockRecord two data structures that knowledge is the key to understanding the underlying principles of the synchronized keyword.

  Section II: Analysis biased locking lock unlock timing and process

A. The first to understand two data structures, you should understand the knowledge points

1.MarkWord: to make the corresponding operation in the course of the lock will lock object

 In HotSpot virtual machine, Java objects stored in memory layout is divided into three parts: the object header, instance data, alignment padding.

This article focuses on the object head.

Object header is divided into two or three parts, comprises:

  1. MarkWord (hereinafter referred to as MW, subsequent detail)
  2. Type pointers: a pointer to this data class metadata object belongs (Klass) of
  3. This last part is rather special, only in the presence of the object is only a Java array, records the length of the array. Why there is this record it? We know that, in an ordinary Java objects, we can read the metadata of the object's class, the calculated size of the object. The array can not be done, so help this area to record.

This article focuses MW area

MW is a fixed size memory area is bit 32 in the 32-bit virtual machine, corresponding to the virtual machine 64 is 64 bit. In this paper, an example to analyze 32-bit virtual machines.

We understand intuitively, so-called header information used are generally described some of the information is not easily deformed, for example, various header information in the header request in http. The same is true in the object header, e.g. hashcode. In order to solve the JVM virtual machine memory space overhead, MW object header has a fixed size. So, to have more information stored, including but not limited to: Lock flag, GC information, lock information, the total size far beyond 32bit, how to do it?

Shared memory region, at different times, according to the information needs of storage needs.

Please refer to the following chart:

Lock Type

25bit

4bit

1bit

2bit

 

23bit

2bit

Whether or not biased locking

Lock flag

no lock

Object hashcode

Generational Age

0

01

Biased locking

Thread ID

epoch

Generational Age

1

01

Lightweight lock

Pointing the stack pointer record lock

00

Heavyweight lock

Pointing mutex

10

GC mark

air

11

 

Description: Two flags can identify up to four states, then the rest how to do a share?. Biased locking and non-locking shared 01 states, the distinction between the two of them

2.LockRecord:

LR request in the current thread's stack (LockRecord abbreviated, hereinafter the same), comprising two main parts, the first step portion may be used to store a copy of MW; second portion obj, a point lock object.

 Relationship between the two is represented by the following diagram:

 

 

II. Work how biased locking

When an object is created, MW will have an initial state, either no lock state or the initial state biased locking (ThreadId, epoch values ​​to the initial value 0). Programmer's world there is no ambiguity, will eventually pick one, it is based on the selected configuration parameters of the virtual machine, after JDK1.6, is enabled by default, if you want to disable out: -XX: -UseBiasedLocking.

When do I need to disable it? If the program can be confirmed in most cases, there are multiple threads compete, then you can disable it biased locking. No need to go over every biased locking -> Lightweight lock -> Lock heavyweight complete the upgrade process.

1. FIG first put an intuitive description of the biased locking lock, unlock, the basic flow revocation

 

 

 

 

2. locking procedure

 step one:

  1. LR record assignment: In the current thread's stack, apply a LR, pointing to the lock object obj

Step two: shown in FIG thread T1, to execute the synchronization code, attempts to add biased lock, first lock is biased do [available] determination:

  1. Bit values ​​of the three-bit region after the object header locks the object MW is 101. Of particular note: If 001 is lock-free state, on behalf of bias lock is not available, it will go lock plus a lightweight process.
  2. ThreadId值:
    1. If ThreadId = 0, no representative of any biased locking thread holds the object, lock operation be performed, the process enters the lock;
    2. ! If ThreadId = 0, it is judged whether the value of the current thread ID, two cases: If direct reentrant lock, the lock will not be repeated. If not, that is the other thread (figure T2) has received a synchronization lock, enter the "third step" process lock contention.
  3. epoch value: the object belongs in Class also maintains a epoch value, here we referred to as cEpoch, the value of the judgment could lead to two things:
    1. If the epoch <cEpoch, and ThreadId! = 0, indicating the occurrence of excessive quantities of heavy bias current lock object has been "released" the. At this point a "heavy bias" (says the release is not releasing the true sense, but implies a layer of meaning: the current thread has been executing the synchronized block, and the heavy bias in a particular operation, but also detect this, no longer maintain a current value of the epoch, so that a new thread at this time believe the biased lock, can be locked directly to CAS modify ThreadId).
    2. If ThreadId == 0, the lock is not revoked because the display changes biased ThreadId process described is certainly the initial state, the epoch value 0 is the initial state certainly, this time directly lock operation.

    MW content locked state as shown below:

    

Lock Type

25bit

 

4bit

1bit

2bit

 

23bit

2bit

 

Whether or not biased locking

Lock flag

Biased locking

ThreadId==0

epoch==n

Generational Age

1

01

 

     It is determined by three or more points, into the "second step" process lock

Step two: By CAS atomic operations, the T1 is written ThreadId MW. The results of two situations:

  1. Successfully written, biased locking is obtained, in the synchronization code synchronization logic block executes.
  2. Write failure, indicating that the first step in between the judge and the CAS operation, there are other threads have gained lock. Go lock contention logic.

2. Unlock process

After the completion of the implementation of the current thread synchronization code block, unlock, unlock operation is relatively simple, only the stack LR in a recent assignment obj is null. It should be noted, MW of threadId and not make any changes.

 

 

3. Lock competitive process flow

  T2 thread holding the lock will not be the first time found direct competition revocation lock, or lock upgrade, but the implementation of the safe point after treatment.

  1. At this time, if the current thread is executing the code in the sync blocks have not survived the thread will lock withdrawn to restore the object to the non-lock state of the lock, then the logic enters the lock escalation.
  2. If the current thread synchronization block execution has not been completed or thread still alive, will go lock the upgrade process, the upgrade is a lightweight lock, and the upgrade after T2 continue to hold lightweight lock, continue to perform synchronization code.

 

 

 

  ps: how to determine whether synchronization code still execute it? Traversal stack RL, if all is null, on behalf of the lock have been released.

4. Batch weight and bulk bias revocation

There is a scenario: If we predict much competition, in most cases a single thread of execution sync blocks, opened the biased locking. But in the actual use environment, there has been a lot of competition, this time how to do it? Stop re-configuration parameters? Probably not the best solution. If we are to design the Synchronized lock this would certainly have to do some catch-all strategy. For example, do it this way, when a certain event occurs N times, then change my treatment strategy?

Yes, the basic idea of ​​the same, just better, temporarily leaving a suspense, announced in the next.

 

 

Guess you like

Origin www.cnblogs.com/xyang/p/11698549.html