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 replace synchronized with reentrantlock in java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "how to replace synchronized with reentrantlock in java". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to replace synchronized with reentrantlock in java".

Note: reentrantlock must release the lock manually.

Synchronized is locked manually and released automatically. Reentrantlock is automatically locked and released automatically.

Effect: m2 code can not be executed until M1 is executed:

/ * reentrantlock is used to replace synchronized * because M1 locks this, m2 can only be executed when M1 finishes execution * here is a review of the original semantics of synchronized * * the same function can be done with reentrantlock * it is important to note that the lock must be released manually (say the important thing three times) * if an exception is encountered with syn lock, jvm will automatically release the lock But lock must release the lock manually, so lock release is often done in finally * @ author mashibing * / package yxxy.c_020 Import java.util.concurrent.TimeUnit;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;public class ReentrantLock2 {Lock lock = new ReentrantLock (); void M1 () {/ / write try...catch..finally or try..finally try {lock.lock () / / synchronized (this) for (int I = 0; I < 10; iTunes +) {TimeUnit.SECONDS.sleep (1); System.out.println (I);}} catch (InterruptedException e) {e.printStackTrace () } finally {/ / release lock lock.unlock ();}} void m2 () {/ / lock the same lock to mutually exclusive lock.lock (); System.out.println ("m2...") Lock.unlock ();} public static void main (String [] args) {ReentrantLock2 rl = new ReentrantLock2 (); new Thread (rl::m1). Start (); try {TimeUnit.SECONDS.sleep (1) } catch (InterruptedException e) {e.printStackTrace ();} new Thread (rl::m2) .start () }} Thank you for your reading, the above is the content of "how to replace synchronized with reentrantlock in java". After the study of this article, I believe you have a deeper understanding of how to replace synchronized with reentrantlock in java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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