In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "detailed explanation of reentrant locks in Java". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the detailed explanation of reentrant locks in Java.
In this article, we are talking about reentrant locks in a broad sense, not just ReentrantLock under JAVA.
A reentrant lock, also known as a recursive lock, means that after the outer function of the same thread acquires the lock, the inner recursive function still has the code to acquire the lock, but it is not affected.
Both ReentrantLock and synchronized are reentrant locks in the JAVA environment.
The following are examples of usage:
Package reentrantLock; public class Test implements Runnable {public synchronized void get () {System.out.println (Thread.currentThread (). GetId ()); set ();} public synchronized void set () {System.out.println (Thread.currentThread (). GetId ());} @ Override public void run () {get ();} public static void main (String [] args) {Test ss=new Test (); new Thread (ss). Start ();}}
Package reentrantLock; import java.util.concurrent.locks.ReentrantLock; public class Test implements Runnable {ReentrantLock lock = new ReentrantLock (); public void get () {lock.lock (); System.out.println (Thread.currentThread (). GetId ()); set (); lock.unlock ();} public void set () {lock.lock (); System.out.println (Thread.currentThread (). GetId ()); lock.unlock ();} @ Override public void run () {get () } public static void main (String [] args) {Test ss = new Test (); new Thread (ss). Start ();}}
The greatest function of reentrant locks is to avoid deadlocks.
Let's take spin lock as an example.
Public class SpinLock {private AtomicReference owner = new AtomicReference (); public void lock () {Thread current = Thread.currentThread (); while (! owner.compareAndSet (null, current)) {} public void unlock () {Thread current = Thread.currentThread (); owner.compareAndSet (current, null);}}
For spin locks
1. If there are two calls to lock () by the same thread, it will cause the second call to the lock position to spin, resulting in a deadlock, indicating that the lock is not reentrant. (within the lock function, verify that the thread is a thread that has acquired the lock.)
2. If problem 1 has been solved, the lock has been released when unlock () is called for the first time. The lock should not actually be released. (count times for statistics) after modification, it is as follows:
Public class SpinLock1 {private AtomicReference owner = new AtomicReference (); private int count = 0 leading public void lock () {Thread current= Thread.currentThread (); if (current==owner.get ()) {count++;return;} while (! owner.compareAndSet (null, current)) {}} public void unlock () {Thread current= Thread.currentThread (); if (current==owner.get ()) {if (countkeeper 0) {count--;} else {owner.compareAndSet (current, null);}}}
The spin lock is a reentrant lock.
At this point, I believe you have a deeper understanding of the "detailed explanation of reentrant locks in Java". 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.