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

What is the upgrade strategy for Java locks?

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

Share

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

This article mainly explains "what is the upgrade strategy of Java lock". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the upgrade strategy of Java locks?"

These three locks refer to the state of the lock and are specific to the Synchronized keyword. In order to reduce the performance consumption of "heavyweight lock", JDK 1.6 introduces "biased lock" and "lightweight lock". The lock has four states: no lock state, bias lock, lightweight lock and heavy lock. The lock state is marked by the Mark Word of the object header:

Locks can be upgraded but cannot be degraded, which means that biased locks cannot be degraded to biased locks after upgrading to lightweight locks. This strategy of upgrading locks but not downgrading them is to improve the efficiency of acquiring and releasing locks.

Heavyweight locks: threads are blocked depending on the Mutex Lock of the underlying operating system

Disadvantages: locking and unlocking requires switching from user mode to kernel mode, which consumes a lot of performance

Lightweight lock: optimized based on heavyweight locks (avoiding context switching and improving performance), it assumes that multithread contention is staggered with each other and thread blocking does not occur, so context switching is redundant

The first feature: CAS operation is used to lock and unlock. Because the lock records (Lock Record) of lightweight locks are stored in object headers and thread space, locking and unlocking do not require context switching and consume less performance.

The second feature: once multithreaded competition occurs, first of all, based on the idea of "spin lock", the spin CPU loop waits for a period of time without context switching, and if the lock is still not available, the lock will be upgraded to a heavy lock.

Biased locking: optimized based on lightweight locks (reducing multiple locking and unlocking, improving performance), it assumes that only one thread gets the lock throughout the process, so multiple locking and unlocking is superfluous

Features: the lock will not be released after the lock is acquired for the first time, it will always hold the lock, and when entering the lock later, you only need to check whether the lock status and bias thread ID is yourself, thus saving many times of locking and unlocking.

1. Bias lock

Acquire the lock:

Check whether the Mark Word of the object header is biased (that is, whether it is biased lock 1 and whether the lock flag bit is 01). If not, try a competitive lock: try the CAS operation to set the thread ID of Mark Word to the current thread ID to indicate that the thread has acquired the lock. If it fails, the lock is occupied.

If the state is biased, check whether the thread ID is the current thread ID. If so, the current thread already holds the lock (the lock is reentrant), otherwise the lock is occupied.

If the lock is occupied, you can only undo the bias lock to no lock or lightweight lock

Release locks: (biased locks use a mechanism that waits for competition to release locks. Threads do not actively release biased locks, and threads holding biased locks release locks only when other threads compete for biased locks.)

The revocation of the biased lock needs to wait for the global safe point (there is no executing bytecode at this point in time), pause the thread with the biased lock and check to see if the thread holding the biased lock is still alive.

If the thread dies, the object header is set to unlocked; if the thread is still alive, the object header is set to a lightweight lock (lock upgrade), and eventually the lightweight lock must be released

two。 Lightweight lock

Acquire the lock:

Check whether the Mark Word of the object header is a lightweight lock (lock flag bit is 00), and if not, try a contention lock: JVM first establishes a lock record (Lock Record) in the stack frame of the current thread, which is used to back up the Mark Word of the storage object header (officially prefixed with a Displaced, called Displaced Mark Word), and then JVM attempts the CAS operation to update the Mark Word to a pointer to Lock Record to indicate that the thread has acquired the lock If it fails, the lock is occupied.

If it is a lightweight lock, determine whether the Mark Word of the object header points to the Lock Record of the stack frame of the current thread. If so, it indicates that the current thread already holds the lock (the lock is reentrant), otherwise the lock is occupied.

If the lock is occupied, the current thread tries to spin the CPU to acquire the lock. After a certain number of spins, the lightweight lock expands to a heavyweight lock (the lock flag bit changes to 10), and the thread enters blocking.

Release the lock:

Try the CAS operation to replace the Displaced Mark Word back to the object header. If successful, the lightweight lock is released successfully.

If the CAS operation fails, it indicates that there is a lock competition, and the lock has been expanded to a heavy lock. It is necessary to wake up those suspended threads while releasing the lock.

3. Weight lock

The heavy lock depends on the Mutex Lock of the underlying operating system, all threads will be blocked, and the switching between threads needs to be from user mode to kernel mode, so the switching cost is very high.

Summary: comparison of advantages and disadvantages of locks

Biased lock (Biased Lock) locking and unlocking does not require additional consumption, and there is only a nanosecond gap compared with the execution of asynchronous methods. If there is lock competition between threads, it will lead to additional lock revocation. Threads with only one thread accessing lightweight lock (Lightweight Lock) competition will not block, improving the response speed of the program. For threads that do not get locks, spin will consume CPU pursuit response time. Or require the critical section to be short, spin will not occupy CPU for too long heavy lock (Heavyweight Lock) threads compete not to use spin, will not consume CPU resources thread blocking, slow response time to pursue throughput

At this point, I believe you have a deeper understanding of "what is the upgrade strategy of Java locks". 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