Get the App
SLTechnology News&Howtos  ›  Development  › 

Operation methods of synchronized and ReentrantLock reentrant lock verification

Shulou Source: shulou.com Published: 2022-06-03 11:01:27 09月30日 Update

Today, I will talk to you about the operation methods of synchronized and ReentrantLock reentrant lock verification, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

If a thread has acquired a lock, it can acquire the acquired lock many times without deadlock (analogous to the example of a life gate lock).

Preface

A reentrant lock, also known as a recursive lock, means that when the same thread acquires the lock in the outer method, the inner method of the thread automatically acquires the lock (provided that the lock object must be the same object). It will not be blocked because it has been acquired before and has not been released. Both ReentrantLock and synchronized in Java are reentrant locks. One of the advantages of reentrant locks is that deadlocks can be avoided to some extent.

1. Synchronized can reenter and verify package com.lau.javabase.lock;/** * implicit lock * / public class ReenterSynchronizedTest {public synchronized void print () {System.out.println ("outer call..."); / / middle layer call print2 (); / / inner layer call print3 ();} public synchronized void print2 () {System.out.println ("middle layer call...") } public synchronized void print3 () {System.out.println ("inner layer call...");} public static void main (String [] args) {ReenterSynchronizedTest test = new ReenterSynchronizedTest (); / / test.print (); test.print4 () } / * the same thread can acquire the same lock multiple times * / public void print4 () {synchronized (this) {System.out.println ("outer layer call..."); synchronized (this) {System.out.println ("middle layer call...") Synchronized (this) {System.out.println ("inner layer calls...");}

Output:

The outer layer is called. Mid-level call. The inner layer is called. 2. ReenTrantLock reentrant verification package com.lau.javabase.lock;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;/** * display lock * / public class ReenterLockTest {public static void main (String [] args) {ReenterLockTest test = new ReenterLockTest (); test.print (); new Thread (()-> test.print2 (), "t") .start () } private Lock lock = new ReentrantLock (); / * the same thread can acquire the same lock multiple times * / public void print () {try {lock.lock (); lock.lock (); System.out.println ("outer call"); try {lock.lock () System.out.println ("middle layer call"); try {lock.lock (); System.out.println ("inner layer call");} finally {lock.unlock () }} finally {lock.unlock ();}} finally {lock.unlock (); / / lock.unlock ();}} public void print2 () {try {lock.lock () System.out.println ("come on!") ;} finally {lock.unlock ();}}

Output:

After reading the above, do you have any further understanding of the operation methods of synchronized and ReentrantLock reentrant lock verification? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Inner layer middle layer outer layer thread method validation content object deadlock output advantage premise preface alias come on time more knowledge example degree Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Apple Microsoft Xiaomi Shulou Technology