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 java tries to lock using tryLock

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 "java how to use tryLock to try to lock", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "java how to use tryLock to try to lock" it!

Synchronized will have to wait if he doesn't get the lock. Reentrantlock is a little more flexible than synchronized.

/ * 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 the lock must release the lock manually, so the lock is often released in the finally * * you can use reentrantlock to "try to lock" tryLock, so it cannot be locked or cannot be locked within a specified period of time, and the thread can decide whether to continue to wait for * @ 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 ReentrantLock3 {Lock lock = new ReentrantLock (); void M1 () {try {lock.lock (); for (int I = 0; I < 10) Catch +) {TimeUnit.SECONDS.sleep (1); System.out.println (I);}} catch (InterruptedException e) {e.printStackTrace ();} finally {lock.unlock () }} / * use tryLock to attempt locking, regardless of whether it is locked or not, the method will continue to execute * you can determine whether to lock according to the return value of tryLock * you can also specify the time of tryLock. Because tryLock (time) throws an exception, you should pay attention to the handling of unclock. Must be put in finally * / void m2 () {/ * boolean locked = lock.tryLock () System.out.println ("m2..." + locked); if (locked) lock.unlock (); * / boolean locked = false Try {/ / try to wait 5 seconds for a lock locked = lock.tryLock (5, TimeUnit.SECONDS); System.out.println ("m2..." + locked);} catch (InterruptedException e) {e.printStackTrace () } finally {if (locked) lock.unlock ();}} public static void main (String [] args) {ReentrantLock3 rl = new ReentrantLock3 (); new Thread (rl::m1) .start () Try {TimeUnit.SECONDS.sleep (1);} catch (InterruptedException e) {e.printStackTrace ();} new Thread (rl::m2) .start () }} Thank you for reading, the above is the content of "how java uses tryLock to try to lock". After the study of this article, I believe you have a deeper understanding of how java uses tryLock to try to lock, 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