Get the App
SLTechnology News&Howtos  ›  Development  › 

Detailed explanation of reentrant lock in Java

Shulou Source: shulou.com Published: 2022-06-03 15:40:52 09月13日 Update

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!

Tags: Thread function content actual deadlock recursion learning practical deeper maximum code location function example interest inner layer outer layer instance practicality actually Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia MySQL Docker Shulou Tech Info Shulou Technology