【多线程】Thread.interrupted()与thread.isInterrupted()的区别

在Java的线程基本操作方法中,有两种方式获取当前线程的isInterrupt属性。一种是对象方法thread.isInterrupted(),另一种是Thread类的静态方法Thread.interrupted()。这两个方法看似相同,实际上是有区别的,我们来看看Java的Thread类的这部分源代码:

public
class Thread implements Runnable {
    ......

    public static boolean interrupted() {
        return currentThread().isInterrupted(true);
    }

    public boolean isInterrupted() {
        return isInterrupted(false);
    }

    /**
     * Tests if some Thread has been interrupted.  The interrupted state
     * is reset or not based on the value of ClearInterrupted that is
     * passed.
     */
    private native boolean isInterrupted(boolean ClearInterrupted);

    ......
}

可以看到,对象方法的thread.isInterrupted()和静态方法的Thread.interrupted()都是调用的JNI底层的isInterrupted()方法。但是区别在于这个ClearInterrupted参数,前者传入的false,后者传入的是true。相信各位读者都已经猜出其中的含义了,ClearInterrupted参数向操作系统层指明是否在获取状态后将当前线程的isInterrupt属性重置为(或者叫恢复,或者叫清除)false。

这就意味着当某个线程的isInterrupt属性成功被置为true后,如果您使用对象方法thread.isInterrupted()获取值,无论您获取多少次得到的返回值都是true;但是如果您使用静态方法Thread.interrupted()获取值,那么只有第一次获取的结果是true,随后线程的isInterrupt属性将被恢复成false,后续无论使用Thread.interrupted()调用还是使用thread.isInterrupted()调用,获取的结果都是false

=====================================================================

调用Thread.interrupt()方法并不能真正停止线程,只是在当前线程做了一个中断的状态标志。

public class MyThread extends Thread{

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            super.run();
            System.out.println("i="+(i+1));
        }
    }

}
public class Runner {
    public static void main(String[] args) {

            MyThread myThread = new MyThread();
            myThread.start();
            myThread.interrupt();
            System.out.println("第一次调用myThread.isInterrupted(),返回值:"+myThread.isInterrupted());
            System.out.println("第二次调用myThread.isInterrupted(),返回值:"+myThread.isInterrupted());
            System.out.println("===========end=============");
    }

}

上面我们创建了一个MyThread线程,然后Runner类中,执行main方法,创建MyThread的实例,启动线程,然后调用myThread.interrupt(); 
打印出信息:

i=1 
第一次调用myThread.isInterrupted(),返回值:true 
i=2 
第二次调用myThread.isInterrupted(),返回值:true 
i=3 
==========end============== 
i=4 
i=5 
i=6 
i=7 
i=8 
i=9 
i=10 
i=11 
i=12 
i=13 
i=14

从打印信息可以看出,虽然调用了myThread.interrupt()方法,但是MyThread并没有立即中断执行。这里我们两次调用myThread.isInterrupted(),返回值都是true。 
我们对Runner方法坐下修改,改成两次调用myThread.interrupted()。 
代码如下:

public class Runner {
    public static void main(String[] args) {

            MyThread myThread = new MyThread();
            myThread.start();
            myThread.interrupt();
            System.out.println("第一次调用myThread.interrupted(),返回值:"+myThread.interrupted());
            System.out.println("第二次调用myThread.interrupted(),返回值:"+myThread.interrupted());
            System.out.println("============end===================");
    }

}

打印信息如下:

第一次调用myThread.interrupted(),返回值:false 
i=1 
第二次调用myThread.interrupted(),返回值:false 
i=2 
===========end================= 
i=3 
i=4 
i=5 
i=6

虽然线程依然没有被中断,但是调用myThread.interrupted()是,返回都是false。难度是MyThread并没有中断状态吗?

再看一下代码:

public class Runner {
    public static void main(String[] args) {

            Thread.currentThread().interrupt();
            System.out.println("第一次调用Thread.interrupted(),返回值:"+Thread.interrupted());
            System.out.println("第二次调用Thread.interrupted(),返回值:"+Thread.interrupted());
            System.out.println("=================end===============================");
    }

}

打印信息如下:

第一次调用Thread.interrupted(),返回值:true 
第二次调用Thread.interrupted(),返回值:false 
========end=======

以上代码是对当前线程,即main方法执行的线程,调用interrunt方法。第一次调用Thread.interrupted()返回值是true,说明当前线程已经被标记了中断状态,那么为什么第二次调用Thread.interrupted()返回值却是false呢?

这里需要引出官方文档对Thread.interrupted()的定义:

测试当前线程是否已经中断,线程的中断状态也是由该方法清除。

也就是说,如果连续两次调用该方法,那么第一次调用时,如果当前线程已经处于中断状态,那么该方法会返回true,同时清除当前线程被标记的中断状态。第二次调用时,(第二次调用之前,没有再次调用Thread.currentThread().interrupt();)就会返回false了。

总结

  1. 调用线程的interrupt方法,并不能真正中断线程,只是给线程做了中断状态的标志
  2. Thread.interrupted():测试当前线程是否处于中断状态。执行后将中断状态标志为false
  3. Thread.isInterrupte(): 测试线程Thread对象是否已经处于中断状态。但不具有清除功能

猜你喜欢

转载自blog.csdn.net/wjx_jasin/article/details/82701185