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

Operation methods of synchronized and ReentrantLock reentrant lock verification

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

Share

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

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.

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