Java技术 interrupt(), interrupted(), isInterrupted()方法区别
1. interrupt()
首先看看官方说明:
interrupt()方法
1 2 3 4 5
|
public void interrupt();
|
其作用是中断此线程
(此线程不一定是当前线程,而是指调用该方法的Thread实例所代表的线程),但实际上只是给线程设置一个中断标志,线程仍会继续运行。
2. interrupted()方法
1 2 3 4 5 6 7
|
public static boolean interrupted()
|
作用是测试当前线程是否被中断(检查中断标志),返回一个boolean并清除中断状态,第二次在调用时中断状态已经被清除,将返回一个false
3. isInterrupted()方法
1 2 3 4 5 6 7
|
public boolean isInterrupted()
|
作用是只测试此线程是否被中断 ,不清除中断状态。
4 测试说明
下面我们进行测试说明:
定义一个MyThread类,继承Thread,如下:
1 2 3 4 5 6 7 8
| public class MyThread extends Thread { @Override public void run() { for (int i = 0; i < 10; i++) { System.out.println("i="+(i+1)); } } }
|
在main方法中测试:
1 2 3 4 5 6 7 8 9 10
| public class Do { public static void main(String[] args ) { MyThread thread=new MyThread(); thread.start(); thread.interrupt(); System.out.println("第一次调用thread.isInterrupted():"+thread.isInterrupted()); System.out.println("第二次调用thread.isInterrupted():"+thread.isInterrupted()); System.out.println("thread是否存活:"+thread.isAlive()); } }
|
输出结果如下:
1 2 3 4 5 6 7 8 9 10 11 12 13
| 第一次调用thread.isInterrupted():true 第二次调用thread.isInterrupted()::true thread是否存活:true i=1 i=2 i=3 i=4 i=5 i=6 i=7 i=8 i=9 i=10
|
从结果可以看出调用interrupt()方法后,线程仍在继续运行,并未停止,但已经给线程设置了中断标志,两个isInterrupted()方法都会输出true,也说明isInterrupted()方法并不会清除中断状态。
下面我们把代码修改一下,多加两行调用interrupted()方法:
1 2 3 4 5 6 7 8 9 10 11 12 13
| public class Do { public static void main(String[] args ) { MyThread thread=new MyThread(); thread.start(); thread.interrupt(); System.out.println("第一次调用thread.isInterrupted():"+thread.isInterrupted()); System.out.println("第二次调用thread.isInterrupted():"+thread.isInterrupted()); System.out.println("第一次调用thread.interrupted():"+thread.interrupted()); System.out.println("第二次调用thread.interrupted():"+thread.interrupted()); System.out.println("thread是否存活:"+thread.isAlive()); } }
|
输出结果如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| 第一次调用thread.isInterrupted():true 第二次调用thread.isInterrupted()::true 第一次调用thread.interrupted():false 第二次调用thread.interrupted()::false thread是否存活:true i=1 i=2 i=3 i=4 i=5 i=6 i=7 i=8 i=9 i=10
|
从输出结果看,可能会有疑惑,为什么后面两个interrupted方法输出的都是false,而不是预料中的一个true一个false?注意!!!这是一个坑!!!上面说到,interrupted()方法测试的是当前线程是否被中断,当前线程!!!当前线程!!!
这里当前线程是main线程,而thread.interrupt()中断的是thread线程,这里的此线程就是thread线程。所以当前线程main从未被中断过,尽管interrupted()方法是以thread.interrupted()的形式被调用,但它检测的仍然是main线程而不是检测thread线程,所以thread.interrupted()在这里相当于main.interrupted()
。对于这点,下面我们再修改进行测试。
Thread.currentThread()函数可以获取当前线程,下面代码中获取的是main线程
1 2 3 4 5 6 7 8 9 10 11
| public class Do { public static void main(String[] args ) throws InterruptedException { Thread.currentThread().interrupt(); System.out.println("第一次调用Thread.currentThread().interrupt():" +Thread.currentThread().isInterrupted()); System.out.println("第一次调用thread.interrupted():" +Thread.currentThread().interrupted()); System.out.println("第二次调用thread.interrupted():" +Thread.currentThread().interrupted()); } }
|
这里都是针对当前线程在操作,如果interrupted()方法有检测中断并清除中断状态的作用,预料中的输出应该是true-true-false,实际输出如下:
1 2 3
| 第一次调用Thread.currentThread().interrupt():true 第一次调用thread.interrupted():true 第二次调用thread.interrupted():false
|
结果证明猜想是正确的。
若果想要是实现调用interrupt()方法真正的终止线程,则可以在线程的run方法中做处理即可,比如直接跳出run()方法使线程结束,视具体情况而定,下面是一个例子。
修改MyThread类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public class MyThread extends Thread { @Override public void run() { for (int i = 0; i < 1000; i++) { System.out.println("i="+(i+1)); if(this.isInterrupted()){ System.out.println("通过this.isInterrupted()检测到中断"); System.out.println("第一个interrupted():"+this.interrupted()); System.out.println("第二个interrupted():"+this.interrupted()); break; } } System.out.println("因为检测到中断,所以跳出循环,线程到这里结束,因为后面没有内容了"); } }
|
测试MyThread:
1 2 3 4 5 6 7 8 9 10
| public class Do { public static void main(String[] args ) throws InterruptedException { MyThread myThread=new MyThread(); myThread.start(); myThread.interrupt(); Thread.currentThread().sleep(1000); System.out.println("myThread线程是否存活:"+myThread.isAlive()); } }
|
结果:
1 2 3 4 5 6
| i=1 通过this.isInterrupted()检测到中断 第一个interrupted():true 第二个interrupted():false 因为检测到中断,所以跳出循环,线程到这里结束,因为后面没有内容了 myThread线程是否存活:false
|
最后总结,关于这三个方法,interrupt()是给线程设置中断标志;interrupted()是检测中断并清除中断状态;isInterrupted()只检测中断。还有重要的一点就是interrupted()作用于当前线程,interrupt()和isInterrupted()作用于此线程,即代码中调用此方法的实例所代表的线程。
原文:https://blog.csdn.net/qq_39682377/article/details/81449451