In this article, we will talk about the interrupt interrupt mechanism of Java thread Thread.
Interrupt thread
The thread.interrupt() method of the thread is to interrupt the thread. It will set the interrupt status bit of the thread, that is, set to true. Whether the interrupted thread dies, waits for a new task or continues to run to the next step depends on the program itself. The thread will detect this interrupt flag from time to time to determine whether the thread should be interrupted (whether the interrupt flag is true). It does not interrupt a running thread like the stop method.
Determine whether the thread is interrupted
To determine whether a thread has sent an interrupt request, please use the Thread.currentThread().isInterrupted() method (because it will not clear the interrupt flag bit immediately after setting the thread interrupt flag bit to true, that is, it will not set the interrupt flag to false). Instead, do not use the thread.interrupted() method (the interrupt flag bit will be cleared after the method is called, that is, reset to false) method to judge. The following is the interrupt method of the thread when it is in a loop:
while(!Thread.currentThread().isInterrupted() && more work to do){ do more work}Interrupt status flag
There are the following methods in the interrupt interrupt mechanism:
Therefore, the interrupt interrupt mechanism does not really interrupt the current thread, but a change in the interrupt markup. Let's test it with examples first.
public class InterruptTest { //The time consumed here is used to print private static long time = 0; private static void resetTime(){ time = System.currentTimeMillis(); } private static void printContent(String content){ System.out.println(content + " Time: " + (System.currentTimeMillis() - time)); } public static void main(String[] args) { test1(); } private static void test1(){ Thread1 thread1 = new Thread1(); thread1.start(); //Interrupt interrupt after 3 seconds of delay try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } thread1.interrupt(); printContent("Execution interrupt"); } private static class Thread1 extends Thread{ @Override public void run() { resetTime(); int num = 0; while (true){ if(isInterrupted()){ printContent("Current thread isInterrupted"); break; } num++; if(num % 100 == 0){ printContent("num : " + num); } } } } } }}The above code is to start a Thread1 thread, and continuously add 1 to num in the while loop of the Thread1 thread, and print it once every multiple of 100 (prevent the printing from too fast). Then, after sleeping for 3000 milliseconds, the main thread calls the interrupt method of the Thread1 thread. Then let's look at the output:
interrupt interrupt
It can be seen that after taking about 3000 milliseconds, that is, after the main thread sleeps, the Thread1 thread stops, and the Thread1 thread stops because the isInterrupted method in the while loop returns true, so break exits the while loop. That is to say, the role of interrupt and isInterrupted here is equivalent to the role of setXX and getXX, maintaining a boolean variable.
interrupt exception handling
Of course, the interrupt mechanism is not just a change and detection of interrupt status bits, it can also handle interrupt exceptions. We know that the Thread.sleep() method needs to catch the interrupt exception, so let's add a sleep delay to it and try it
while (true){ if(isInterrupted()){ printContent("current thread isInterrupted"); break; } num++; //sleep try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } if(num % 100 == 0){ printContent("num : " + num); } }Let's look at the output result:
interrupt interrupt
Here we will find that after sleeping, the output num value is significantly smaller (nums reach 1 billion when they are not sleeping, and it seems that the CPU performs simple operations very quickly). Haha, but this is not the point. The point is that after the output exception, the isInterrupted output returns false, and the Thread1 thread continues to execute, and does not exit the while loop. So why is this? We just added a sleep.
If there is an operation in the Thread1 thread that needs to catch the InterruptedException exception, such as Thread's sleep, join method, Object's wait, Condition's await, etc., it forces the InterruptedException exception to catch, then when the thread1.interrupt method is called, it will throw an InterruptedException exception to the thread1 thread. Then in the while loop, this exception can be caught and then after this exception is thrown, the thread interrupt flag will be reset to false immediately. Therefore, when it isInterrupted in the next time the while loop is determined to be false, it will not break, and then the while loop will continue to execute.
Therefore, the interrupt() method will perform different operations based on whether there is code in the run method in the thread thread that must catch the InterruptedException:
Application scenarios of interrupt
Usually interrupt is suitable for loop mark judgments in thread execution, for example
while(!isInterrupted()){ ...} However, if there is a blockage in this loop, the thread cannot judge the next isInterrupted tag, and even if the interrupt() method is called, it cannot exit the loop, and the thread cannot exit. For example
while(!isInterrupted()){ ... while(true){ //The thread is stuck here, the interrupted mechanism cannot be responded to}}In this way, interrupt will be helpless, and the thread will continue to execute and will not be interrupted and stopped.
Test examples to view my GitHub--JavaTest
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.