Interruption and termination of threads
1、interrupt()
、isInterrupted()
、## The role of #interrupted()
method interrupts it. The thread can determine whether it has been interrupted by calling the isInterrupted()
method. The thread can also call the of
Thread interrupted()
Static method resets the interrupt flag bit of the current thread. Related video tutorial recommendations:
As follows:
public class InterruptThread extends Thread{ @Override public void run() { //一个死循环 while (true){ System.out.println("InterruptThread正在执行"); } } } public static void main(String[] args) throws InterruptedException { InterruptThread interruptThread = new InterruptThread(); interruptThread.start(); interruptThread.interrupt();//调用线程的interrupt() System.out.println("interruptThread是否被中断,interrupt = " + interruptThread.isInterrupted()); //此时isInterrupted()方法返回true } 输出结果: interruptThread是否被中断,interrupt = true InterruptThread正在执行 InterruptThread正在执行 InterruptThread正在执行 //...
You can see that when you call the interrupt() method of the thread, calling the isInterrupted() method will return true, but the thread will still continue. Carry on. So how can we terminate the running of a thread?
2. Terminate the running of the threadA thread will automatically end after executing the run method normally. If an exception occurs during the running process, it will also end early; so use In these two cases, we can also safely terminate the running thread in the following three ways:
2.1. Using the interrupt flag bitThe interrupt mentioned earlier The operation can be used to cancel the thread task, as follows:
public class InterruptThread extends Thread{ @Override public void run() { while (!isInterrupted()){//利用中断标记位 System.out.println("InterruptThread正在执行"); } } }
When there is no need to run the InterruptThread thread, by calling InterruptThread.interrupt() so that isInterrupted() returns true, the thread can exit the loop and the normal execution is completed. It ends automatically after that.
2.2. Using a boolean variableUsing a boolean variable is the same as the above method, as follows:
public class InterruptThread extends Thread{ private volatile boolean isCancel; @Override public void run() { while (!isCancel){//利用boolean变量 System.out.println("InterruptThread正在执行"); } } public void cancel(){ isCancel = true; } }
When there is no need to run the InterruptThread thread By calling InterruptThread.cancel() to make isCancel equal to true, the thread can exit the loop and end automatically after normal execution. Note here that the boolean variable must be modified with volatile to ensure the visibility of the memory.
2.3. Respond to InterruptedExceptionWhen interrupting a thread by calling its interrupt(), if the thread is blocked, waiting for a limited time, or waiting indefinitely, Then an InterruptedException will be thrown, thus ending the thread early. For example, when you call the Thread.sleep() method, you will usually be asked to catch an InterruptedException, as follows:
public class InterruptThread extends Thread{ @Override public void run() { try{ while (true){ Thread.sleep(100);//Thread.sleep会抛出InterruptedException System.out.println("InterruptThread正在执行"); } }catch (InterruptedException e){ e.printStackTrace(); } } }
When there is no need to run the InterruptThread thread , by calling InterruptThread.interrupt() to cause Thread.sleep() to throw InterruptedException, the thread can exit the loop and end early. Before throwing an InterruptedException, the JVM will reset the interrupt flag bit, and the calling thread's isInterrupted() method will return false.
Recommended java related articles and tutorials:
Introduction to java programmingThe above is the detailed content of Interruption and termination of threads in java. For more information, please follow other related articles on the PHP Chinese website!