zoukankan      html  css  js  c++  java
  • 【JAVA多线程】interrupted() 和 isInterrupted() 的区别

    Thread 类中提供了两种方法用来判断线程的状态是不是停止的。就是我们今天的两位主人公 interrupted() 和 isInterrupted() 。

    interrupted()

    image_1as4prvqmf31unj13asakincvm.png-21.5kB

    官方解释:测试当前线程是否已经中断,当前线程是指运行 this.interrupted() 方法的线程 。

    public class t12 {
        public static void main(String[] args) {
            try {
                MyThread12 thread = new MyThread12();
                thread.start();
                Thread.sleep(500);
                thread.interrupt();
                System.out.println("是否终止1? =" + thread.interrupted());
                System.out.println("是否终止2? =" + thread.interrupted());
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("-------------end-------------");
        }
    }
    
    class MyThread12 extends Thread {
        public void run() {
            for (int i = 0; i < 50000; i++) {
                System.out.println("i = " + i);
            }
        }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    image_1as4q86481d2u10uo1q3enfnagk1t.png-8.8kB

    程序运行结束后,结果如上图所示,thread 线程并没有停止,而且调用 thread.interrupted() 结果是两个 false 表示线程一直在运行过程中,为什么会出现这种结果呢?

    让我们再来自己看看官方解释,这次请着重阅读我标红的文字:当前线程是指运行 this.interrupted() 方法的线程 。也就是说,当前线程并不是 thread,并不是因为 thread 调用了 interrupted() 方法就是当前线程。当前线程一直是 main 线程,它从未中断过,所以打印结果就是两个 false。

    那么如何让 main 线程产生中断效果呢?

    public class t13 {
        public static void main(String[] args) {
            Thread.currentThread().interrupt();
            System.out.println("是否终止1? =" + Thread.interrupted());
            System.out.println("是否终止2? =" + Thread.interrupted());
            System.out.println("----------end-----------");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    image_1as4qopti13m4lqqivjb3imf12a.png-7.6kB

    从上述结果中可以看出,方法 interrupted() 的确判断出当前线程是否已经停止,但是为什么第 2 个布尔值是 false 呢?让我们重新阅读一次文档。

    image_1as4qsebm1pvo1tv21laq1kqb1jap2n.png-21.6kB

    你看,文档中说的很详细,interrupted() 方法具有清除状态的功能,所以第二次的时候返回值是 false。


    isInterrupted()

    image_1as4psmlbgfhhhgmm31as71dnp13.png-16.1kB

    从声明中可以看出来 isInterrupted() 方法并不是 static 的。并且 isInterrupted() 方法并没有清除状态的功能。

    public class t14 {
        public static void main(String[] args) {
            try {
                MyThread14 thread = new MyThread14();
                thread.start();
                Thread.sleep(1000); //此方法代表 让当前线程休眠 1 秒,即表示使 main线程休眠 1秒
                thread.interrupt();
                System.out.println("是否终止1? =" + thread.isInterrupted());
                System.out.println("是否终止2? =" + thread.isInterrupted());
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("----------end-----------");
        }
    }
    
    class MyThread14 extends Thread {
        public void run() {
            for (int i = 0; i < 500000; i++) {
                System.out.println("i = " + i);
            }
        }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    image_1as4sik741tdg96f1t7n75g1do43h.png-4.3kB


    总结

    1. interrupted():测试 当前线程 是否已经是中断状态,执行后具有清除状态功能

    2. isInterrupted():测试线程 Thread 对象 是否已经是中断状态,但不清楚状态标志

  • 相关阅读:
    Rolling File Appender使用示例
    log4net生成dll文件
    看涨期权(call options)
    log4net file Appender使用示例
    log4net不能记录日志,IsErrorEnabled值为false
    C#委托
    打印事件处理顺序
    Zigbee、WiFi和433MHz无线技术
    log4net Tutorial
    安装服务出现异常
  • 原文地址:https://www.cnblogs.com/forfreewill/p/9111162.html
Copyright © 2011-2022 走看看