Java concurrency: interrupt a running thread and java concurrent interrupt thread
It is not easy to make tasks and threads stop securely and reliably. java does not provide any mechanism to safely terminate threads. What should we do?
The following describes the interrupt mechanism in java:
Java interrupt mechanism is a collaboration mechanism, that is, a thread calls another threadInterrupt ()The thread cannot be terminated directly. The thread that needs to be interrupted must handle the interrupt request by itself.
Interrupted ()AndIsInterrupted ()Main differences:
The non-static method isInterrupted () is used to query the interrupt status of a thread and does not change the identifier of the thread's interrupt status;
Static MethodInterrupted ()When you check the interruption status, the interruption status is cleared (set to false ).
In the following example, the main thread calls t. the interrupt () method sets the thread t interruption status to true. The thread t can call the interrupted () method or isInterrupted () method when appropriate () method to detect the disconnection status and perform corresponding processing:
Package com. test; public class ThreadTest extends Thread {public void run () {while (true) {if (Thread. interrupted () {System. out. println ("Someone interrupted me. "); break;} else {System. out. println ("Going... ");} long now = System. currentTimeMillis (); while (System. currentTimeMillis ()-now <1000) {// Let the loop last for a period of time, fewer output statements will be printed} public static void main (String [] args) throws InterruptedException {ThreadTest t = new ThreadTest (); t. start (); Thread. sleep (3000); t. interrupt ();}}
The result is as follows:
Going...Going...Going...Someone interrupted me.
(1) For non-blocking threads, the interrupt state is changed, that is, Thread. isInterrupted () will return true;
(2) For threads in the canceled blocking status, such as waiting in the Thread. sleep (), Object. wait (), Thread. join (), the threads on these functions. After receiving the interrupt signal, this thread will throw InterruptedException and set the interrupt status to false.
The following program will end the main Thread blocking the Thread. join () method after receiving the interrupt signal:
Package com. test; public class ThreadTest extends Thread {private Thread parent; public ThreadTest (Thread parent) {this. parent = parent;} public void run () {while (true) {System. out. println ("sub thread is running... "); long now = System. currentTimeMillis (); while (System. currentTimeMillis ()-now <2000) {// Let the loop last for a period of time, and fewer output statements will be printed} parent. interrupt () ;}} public static void main (String [] args) {ThreadTest t = new ThreadTest (Thread. currentThread (); t. start (); try {t. join ();} catch (InterruptedException e) {System. out. println ("Parent thread will die... ");}}}
The result is as follows:
sub thread is running...Parent thread will die...sub thread is running...sub thread is running...sub thread is running...sub thread is running...
The result shows that the main thread ends after thread t sends the interrupt request (at this time, thread t is still running)