Hello, THIS is Yes.

A previous article about Synchronized, a point that 99% of articles on the web were wrong, was posted and a lot of readers talked about me in private, feeling that it broke the understanding of lightweight lock upgrade.

Then a reader’s comment reminded me that there was another point about Synchronized that many people may be wrong about.

I was wrong about this until last year when I discussed it with Angela, another host.

We all know that synchronized locks do not degrade after upgrading, so it is natural to assume that when a lock is upgraded to a heavyweight lock, any thread that fights for it will take the heavyweight lock.

It will no longer go from no-lock to bias to lightweight to heavyweight.

So what are the facts? This article we don’t look at the source code, direct whole results.

We directly look at the lock symbol of the lock object to judge.

Go straight to the experimental code.

The code is very simple, first look at the layout of the unlocked object, then multiple threads scramble for what should be a heavyweight lock at this point, and then sleep until all threads finish executing to release the lock, and then look at the layout of the lock at this point.

Add one last lock to look at the object layout.

There’s a caveat here

1.8 the biased locking is delayed effect, have to come into force in JVM startup after 4 seconds, by – XX: closed biased locking delay BiasedLockingStartupDelay = 0

I don’t have this parameter here, because it’s not important, so I won’t have biased locks in it.

The results!

The obvious result is that the initial lock is unlocked.

Then four threads compete to become a heavyweight lock.

After four threads have executed, the lock object becomes unlocked.

At this point, another thread fights for the lock, and the lock becomes lightweight.

So when the heavyweight lock is released, the lock object is unlocked!

New threads to compete will start with lightweight locks again!

Ok, over.

Now take a look at the openJDK Wiki and focus on unlocking on the right!

Is it confirmed?

The last

To test it yourself, reference a jol package

<dependency>  
   <groupId>org.openjdk.jol</groupId>  
   <artifactId>jol-core</artifactId>  
   <version>0.14</version>  
  </dependency>  
Copy the code

The code is also copied directly to you:

public class YesLockTest { static Object yesLock; public static void main(String[] args) throws InterruptedException { yesLock = new Object(); System. The out. Println (" no lock object layout: "+ ClassLayout. ParseInstance (yesLock). ToPrintable ()); IntStream. RangeClosed (1, 4). ForEach (I - > {getYesLock (); }); Thread.sleep(5000L); System. The out. Println (" no competition, the object layout: "+ ClassLayout. ParseInstance (yesLock). ToPrintable ()); getYesLock(); Private static void getYesLock() {new Thread() -> {try {synchronized (yesLock) {private static void getYesLock() {new Thread() -> {try {synchronized (yesLock) { System.out.println(" Thread [" + thread.currentThread ().getName() + "]" + ": heavyweight lock state object layout :" +" ClassLayout.parseInstance(yesLock).toPrintable()); } } catch (Exception e) { e.printStackTrace(); } }).start(); }}Copy the code

I am yes, from a little bit to a billion little bit, welcome to see, forward, leave a message, we will see you next.