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

How to implement a lightweight lock in Java

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Java in how to achieve a lightweight lock, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

When the code enters the synchronization block, if the synchronization object is not locked, that is, the lock flag bit in Mark Word is 01. The virtual machine first creates a space called the lock record in the stack frame of the current thread to store a copy of the current MarkWord of the lock object. The following figure shows the copied thread stack and object header information.

The virtual machine will then use the CAS operation to attempt to update the object's Mark Word to a pointer to the Lock Record. If the update is successful, the thread acquires the lock on the object and changes the lock log bit like Mark Word to 00, which in this case the thread acquires a lightweight lock. At this point, the thread stack and object header information is as follows.

If the above update operation fails, the virtual machine first checks whether the Mark Word of the object points to the stack frame of the current thread. If so, it means that the current thread has acquired the lock of the object, then the virtual machine can execute the code in the synchronized code block directly. If not, the lock object has been acquired by another thread. If more than two threads are competing for the same lock, the virtual machine expands the lightweight lock to a heavyweight lock and changes the state value of the lock flag to 10. The lock pointer stored in Mark Word will be changed to the pointer of the heavyweight lock, and the thread waiting to acquire the lock will enter the blocking state.

The above process is the process by which a thread acquires a lock. Let's take a look at the process by which the thread releases the lock. Thread release of the lock is also done through the CAS operation. When the code in the synchronous code block is executed, the virtual machine checks to see if the object's Mark Word is still pointing to the lock record of the current thread, and if so, replaces the object's current Mark Word and the copied Mark Word in the thread with the CAS operation. If the replacement is successful, the entire synchronization block is executed, that is, the lock has been released. If the replacement fails, it means that another thread is trying to acquire the lock, and other threads that are suspended should be awakened while the lock is released.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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

Internet Technology

Wechat

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

12
Report