In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 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 "how to call the lockInterruptibly method to respond to the threaded interrupt method". Many people will encounter this dilemma in the operation of the actual case, 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 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 the lock is often released in 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 * * using ReentrantLock can also call the lockInterruptibly method, which can respond to the thread interrupt method, * while a thread is waiting for the lock. Can be interrupted * * @ author mashibing * /
The T1 thread occupied the lock.
Public class ReentrantLock4 {public static void main (String [] args) {Lock lock = new ReentrantLock (); Thread T1 = new Thread (()-> {try {lock.lock ()) System.out.println ("T1 start"); TimeUnit.SECONDS.sleep (Integer.MAX_VALUE); System.out.println ("T1 end") } catch (InterruptedException e) {System.out.println ("interrupted!");} finally {lock.unlock ();}}); t1.start () Thread T2 = new Thread (()-> {try {/ / lock.lock (); lock.lockInterruptibly (); / / you can respond to the interrupt () method System.out.println ("T2 start") TimeUnit.SECONDS.sleep (5); System.out.println ("T2 end");} catch (InterruptedException e) {System.out.println ("interrupted!") } finally {lock.unlock ();}}); t2.start (); try {TimeUnit.SECONDS.sleep (1) } catch (InterruptedException e) {e.printStackTrace ();} t2.interrupt (); / / interrupt Thread 2 waiting} "how to call the lockInterruptibly method to respond to the thread interrupt method". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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: 248
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.