在Java并发编程中,对于线程的Thread类中有这样三个方法:interrupt()方法、interrupted()方法和isInterrupted()方法。那么这三个方法的有啥区别呢?首先看一下对这三个方法的解释:
下面对这三个方法进行演示和说明:
首先定义一个线程类,然后在main方法中进行测试,代码如下:
- class TestThread extends Thread{
- @Override
- public void run() {
- for (int i = 1; i <= 5; i++) {
- System.out.println(Thread.currentThread().getName() + "正在打印 i = " + i);
- }
- }
- }
-
- public class TestInterrupted {
- public static void main(String[] args) {
- TestThread mt = new TestThread();
- mt.start();
- mt.interrupt();
- System.out.println("第一次调用isInterrupted()方法,值为:" + mt.isInterrupted());
- System.out.println("第二次调用isInterrupted()方法,值为:" + mt.isInterrupted());
- System.out.println("thread是否存活:" + mt.isAlive());
- }
- }
执行结果如下:

从结果中可以看出:在调用interrupt()方法之后,线程依旧在执行,并没有停止,因为interrupt()方法只是给线程设置了中断标志而已。其次,两次调用的isInterrupted()方法都输出了true,这也佐证了isInterrupted()方法并不会清除线程的中断状态。
接下来对代码进行修改,添加interrupted()方法的调用:
- public class TestInterrupted {
- public static void main(String[] args) {
- TestThread mt = new TestThread();
- mt.start();
- mt.interrupt();
- System.out.println("第一次调用isInterrupted()方法,值为:" + mt.isInterrupted());
- System.out.println("第二次调用isInterrupted()方法,值为:" + mt.isInterrupted());
- System.out.println("调用interrupted()方法,值为:" + Thread.interrupted());
- System.out.println("调用interrupted()方法,值为:" +Thread.interrupted());
- System.out.println("thread是否存活:" + mt.isAlive());
- }
- }
执行结果如下:

从执行结果来看,interrupted()方法的执行结果好像和我们预期的结果不一样。前面提到:interrupted()方法连续两次调用的返回结果不一样,因为第二次调用的时候线程的中断状态已经被清除,会返回一个false。按这个来说的话,这里的执行结果应该是一个true,一个false。
这里有一个注意点:interrupted()方法测试的是当前线程是否被中断。但是这里的当前线程是main线程,而mt.interrupted()中断的是mt线程,而不是main线程。也就是说,当前线程main并没有被中断过,即Thread.interrupted()方法其实相当于main.interrupted(),而Thread.interrupted()方法检测的是mt线程有没有被中断。
来看下面的代码:
- public class TestInterrupted {
- public static void main(String[] args) {
- Thread.currentThread().interrupt();
- System.out.println("第一次调用isInterrupted()方法,值为:" + Thread.currentThread().isInterrupted());
- System.out.println("调用interrupted()方法,值为:" + Thread.currentThread().interrupted());
- System.out.println("调用interrupted()方法,值为:" +Thread.currentThread().interrupted());
- }
- }
执行结果如下:

上面都是针对当前线程进行操作,其运行结果也符合我们的预期:true-true-false,因为interrupted()方法是有检测中断并清除中断状态的作用。
那么,如何才能中断mt线程呢?
如果想要是通过调用interrupt()方法真正的终止线程,则可以在线程的run方法中做处理即可,比如直接跳出run()方法使线程结束,视具体情况而定,下面是一个例子。
-
- class TestThread extends Thread{
- @Override
- public void run() {
- for (int i = 1; i <= 5; i++) {
- System.out.println(Thread.currentThread().getName() + "正在打印 i = " + i);
- if(this.isInterrupted()){//检查中断状态
- System.out.println("通过this.isInterrupted()检测到中断");
- System.out.println("第一个interrupted()"+this.interrupted());
- System.out.println("第二个interrupted()"+this.interrupted());
- break;
- }
-
- }
- System.out.println("检测到线程中断,线程执行结束");
- }
- }
-
- public class TestInterrupted {
- public static void main(String[] args) throws InterruptedException {
- TestThread mt = new TestThread();
- mt.start();
- mt.interrupt();
- Thread.sleep(2000);
- System.out.println("mt线程是否存活:" + mt.isAlive());
- }
- }
执行结果如下:

总结: