Thread的interrupted()和isInterrupted()的区别

近来复习多线程问题的时候,发现Thread.interrupted()和Thread.isInterrupted()功能比较类似,想对它们进行一下区分。

结论:

共性:

调用线程的interrupted()和isInterrupted()方法,只是接受了别的线程传过来的信号,给线程做了中断状态的标志,它们本身不能抛出InterruptedException,就不能直接对线程进行终止,但是可以利用它们来检测我们是否接受到了中断信号。

区别:

Thread.interrupted():测试当前线程是否处于中断状态,执行后将中断状态标志为false。

Thread.isInterrupted(): 测试线程Thread对象是否已经处于中断状态,但不具有标记清除功能。

也就是说,如果连续两次调用interrupted()方法,第一次调用时,如果当前线程接受到了中断信号,那么该方法会返回true,同时清除当前线程被标记的中断状态。第二次调用时,就会返回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======

测试代码部分参考了:

https://blog.csdn.net/fjse51/article/details/53928272



猜你喜欢

转载自blog.csdn.net/Cotria/article/details/80785854