Markword

An object in Java has an object header, which is used to record all sorts of information about the object, Pointers to classes and so on, and an object header, markword, which is used to record lock information. Commonly known as locking the object, is to modify the lock information in Markword.

What does something in MarkWord look like? It’s just a bunch of binaries, as shown below:

Lock escalation

Some bits in Markword are used to indicate the lock status. The specific comparison table is as follows:

Seeing is believing

Object o = new Object();
System.out.println(ClassLayout.parseInstance(t).toPrintable());

synchronized(o) {
	System.out.println(ClassLayout.parseInstance(t).toPrintable());
{
Copy the code

In the code above, let’s look at the object header of object O when it is first created:The red line is 001, which representsunlockedState because it has just been created.

Lock o with synchronized; lock o with synchronized;The red line is zero zero, which is zeroLightweight lockThe number in parentheses represents the thread that owns the lock.

What happens if you have two threads fighting for a lock? The code:

Object o = new Object();
new Thread(() -> {
    synchronized (o) {
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(ClassLayout.parseInstance(o).toPrintable());
    }
}).start();

new Thread(() -> {
    synchronized (o) {
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(ClassLayout.parseInstance(o).toPrintable());
    }
}).start();
Copy the code

Look at the object header:

The red line shows 01, indicating that if two or more threads compete for a lock, the lock will be upgraded to a heavyweight lock

Why is there a lock upgrade process?

So why not just go for the lightweight lock or the heavyweight lock? This is because when a thread executes a block of code that is locked, acquiring the lock consumes resources. Lightweight locks are implemented by CAS spin, which, like a while loop, consumes CPU resources. Heavyweight locks require access by the operating system’s scheduling mechanism and consume resources. And most of the time only one thread work silently, there is no lock competition, every time this lonely threads to execute a lightweight lock lock code need a CAS, performing a heavyweight lock lock code need operating system such as starting, yourself and your competition, the efficiency is a little low. When an object is not already locked, biased locking is labeled directly with a thread ID in MarkWord to indicate that the lock is locked, without time-consuming lock contention mechanisms such as CAS spin and operating system calls, which improves performance. Biased locks assume that the object is not already locked, so biased locks are also called optimistic locks.

So, the lock upgrade process is clear: no lock -> bias lock -> lightweight lock -> heavyweight lock

Resource:

  • Dzone.com/articles/wh…
  • Peel off Java’s skin and see its heart turn over