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

Why ReenTrantLock lock can replace synchronized lock

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

Share

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

This article introduces the knowledge of "Why ReenTrantLock locks can replace synchronized locks". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

ReenTrantLock can replace synchronized lock and is more flexible than synchronized lock.

Synchronized locks are locked and unlocked automatically, while ReenTrantLock needs to be manually locked and unlocked.

Synchronized lock when the program is running, if an exception is thrown, jvm will automatically release the lock, but ReenTrantLock still has to release the lock manually, so the release lock is usually written in finally.

* * the tryLock method of ReentrantLock is to try to acquire the lock. * * is to try to acquire the lock, and continue to execute if you can't get it. You don't want to synchronized the lock. If you can't get the lock, just wait. This method has a return value of boolean type, and you can execute your logic based on this return value. And you can specify the time to attempt to acquire the lock, which is equivalent to the time to wait for the lock to be acquired.

ReentrantLock's lockInterruptibly acquires the lock. In addition to tryLock, the lock can also be acquired through the lockInterruptibly method, which can respond to the thread's interrupt method. The meaning of this method is somewhat similar to the scenario when tryLock uses a timeout. : two threads, T1 gets the lock, T2 thread starts, can't get the lock, and then you don't want to keep T2 waiting. If you use lock or tryLock, you can't interrupt. If you use lockInterruptibly, you can.

ReentrantLock can be a fair lock, which means that whichever thread takes a long time to wait for the lock will be executed first. The synchronized lock is unfair.

Public class ReentrantLockTest {public static void main (String [] args) {Lock rtLock = / * new ReentrantLock (true) Fair Lock * / new ReentrantLock (); Thread T1 = new Thread (()-> {try {rtLock.lock () / / rtLock.tryLock (); try to acquire the lock / / rtLock.tryLock (2 classic TimeUnit. Second); try to acquire the lock and time out to set TimeUnit.SECONDS.sleep (Integer.MAX_VALUE) } catch (InterruptedException e) {e.printStackTrace ();} finally {rtLock.unlock ();}}); t1.start () Thread T2 = new Thread (()-> {try {/ / rtLock.lock (); rtLock.lockInterruptibly (); TimeUnit.SECONDS.sleep (2)) } catch (InterruptedException e) {e.printStackTrace ();} finally {rtLock.unlock ();}}); t2.start () Try {TimeUnit.SECONDS.sleep (2); t2.interrupt ();} catch (InterruptedException e) {/ / TODO Auto-generated catch block e.printStackTrace ();}

ReentrantLock can also bind conditon. You can understand it by looking at the code.

Public class ConditionTest {/ / uses condition to do this: there are 2 producer threads and 10 consumer threads. If the container is full, the producer pauses; if the container is empty, the consumer pauses final private LinkedList list = new LinkedList (); final private int maxValue = 10; Lock lock = new ReentrantLock (); private Condition producerCondition = lock.newCondition () / / producer private Condition consumerCondition = lock.newCondition (); / / Consumer / * producer, as required: stop when the container is full. * / public void producer (Object o) {while (list.size () = = maxValue) {try {/ / this.wait (); wait is producerCondition.await () used with synchronized locks } catch (InterruptedException e) {/ / TODO Auto-generated catch block e.printStackTrace ();}} list.add (o); consumerCondition.signalAll () / / only the consumer thread was awakened} / * * Consumer * / public Object consumer () {while (list.size () = = 0) {try {consumerCondition.await () } catch (InterruptedException e) {/ / TODO Auto-generated catch block e.printStackTrace ();}} Object o = list.removeFirst (); producerCondition.signalAll (); return o } public static void main (String [] args) {ConditionTest conditionTest = new ConditionTest (); / / start the consumer thread for (int iThread 0 I {for (int job0 j)

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