Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Will Synchronized not be able to get down after upgrading to a heavyweight lock?

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

The main content of this article is to explain "whether Synchronized will not come down after upgrading to a heavy lock", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "whether Synchronized can't come down after upgrading to a heavyweight lock"!

We judge by looking directly at the lock sign of the lock object.

Go directly to the experimental code.

The code is very simple, first look at the unlocked object layout, and then multithread scramble for this should be a heavy lock, and then sleep waits for all threads to finish executing and release the lock, and then look at the lock layout at this time.

Finally, add a lock to see the object layout.

Here's a note.

1.8 bias lock will be delayed, it must take effect 4 seconds after JVM startup. Turn off the bias lock delay through-XX:BiasedLockingStartupDelay=0

I didn't do this parameter here, because it's not the key point, so there will be no bias lock in the result later.

Go to the results!

As a result, it is obvious that it is initially unlocked.

Then four threads compete at the same time to become heavyweight locks.

After the execution of four threads, the lock object becomes unlocked.

At this point, another thread scrambles for the lock, which changes from no lock to a lightweight lock.

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

If there are new threads to compete, it will start with lightweight locks!

All right, over.

Last

If you want to test it yourself, just reference a jol package.

Org.openjdk.jol jol-core 0.14

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.out.println ("object layout without locks:" + ClassLayout.parseInstance (yesLock) .toPrintable ())

IntStream.rangeClosed (1B4) .forEach (I-> {getYesLock ();})

Thread.sleep (5000L)

System.out.println ("after no competition, object layout at this time:" + ClassLayout.parseInstance (yesLock) .toPrintable ())

GetYesLock (); / / lock again at this time

}

Private static void getYesLock () {

New Thread (()-> {

Try {

Synchronized (yesLock) {

System.out.println ("thread [" + Thread.currentThread () .getName () + "]" +

": heavy lock state object layout:" + ClassLayout.parseInstance (yesLock) .toPrintable ()

}

} catch (Exception e) {

E.printStackTrace ()

}

}) .start ()

}

}

At this point, I believe you have a deeper understanding of "whether Synchronized will not be able to get down after upgrading to a heavy lock". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report