How to use lockInterruptibly method to realize concurrency in java
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.