In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces how to use lockInterruptibly method to achieve concurrency in java, the content is very detailed, interested friends can refer to, hope to be helpful to you.
The lockInterruptibly method means that the thread can respond to the interrupt request of the thread interrupt method while waiting for the lock to be acquired. In other words, there is a situation where you cannot respond to the interrupt request of the thread's interrupt method while waiting for the lock to be acquired. The synchronized built-in lock is, see the test code:
Public class LockInterruptTest {
Public static void main (String [] args) {
/ / TODO Auto-generated method stub
Thread T1 = new Thread () {
Public void run () {
Synchronized (LockInterruptTest.class) {
While (true) {
Try {
Thread.sleep (1000)
} catch (InterruptedException e) {
E.printStackTrace ()
}
}
}
}
}
T1.start ()
Try {
Thread.sleep (1000)
} catch (InterruptedException E1) {
/ / TODO Auto-generated catch block
E1.printStackTrace ()
}
Thread T2 = new Thread () {
Public void run () {
Synchronized (LockInterruptTest.class) {
While (true) {
Try {
Thread.sleep (1000)
} catch (InterruptedException e) {
E.printStackTrace ()
}
}
}
}
}
T2.start ()
Try {
Thread.sleep (1000)
} catch (InterruptedException E1) {
/ / TODO Auto-generated catch block
E1.printStackTrace ()
}
Thread T3 = new Thread () {
Public void run () {
System.out.println ("prepare to interrupt T2")
T2.interrupt ()
System.out.println ("interrupt T2")
}
}
T3.start ()
}
}
Three threads are prepared in the code, the second thread tries to acquire the lock of the first thread, and the third thread tries to interrupt the second thread. The code can see "interrupt T2" normally, but with the jconsole tool, you can see that the second thread is blocked by trying to acquire the lock:
The code after changing to lockInterruptibly is as follows:
Public class LockInterruptTest {
Static ReentrantLock lock = new ReentrantLock ()
Public static void main (String [] args) {
Thread T1 = new Thread () {
Public void run () {
Try {
Lock.lockInterruptibly ()
} catch (InterruptedException E1) {
E1.printStackTrace ()
}
Try {
While (true) {
Try {
Thread.sleep (1000)
} catch (InterruptedException e) {
E.printStackTrace ()
}
}
} finally {
Lock.unlock ()
}
}
}
T1.start ()
Try {
Thread.sleep (1000)
} catch (InterruptedException E1) {
E1.printStackTrace ()
}
Thread T2 = new Thread () {
Public void run () {
Try {
Lock.lockInterruptibly ()
While (true) {
Try {
Thread.sleep (1000)
} catch (InterruptedException e) {
E.printStackTrace ()
}
}
} catch (InterruptedException E1) {
E1.printStackTrace ()
} finally {
Lock.unlock ()
}
}
}
T2.start ()
Try {
Thread.sleep (1000)
} catch (InterruptedException E1) {
E1.printStackTrace ()
}
Thread T3 = new Thread () {
Public void run () {
System.out.println ("prepare to interrupt T2")
T2.interrupt ()
System.out.println ("interrupt T2")
}
}
T3.start ()
}
}
Through the jconsole tool, you can find that the second thread was successfully interrupted.
On how to use the lockInterruptibly method in java to achieve concurrency is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.
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.