`

interrupted()和isInterrupted()的区别

 
阅读更多

原创转载请注明出处:http://agilestyle.iteye.com/blog/2359893

 

Method in Java Doc


 

调用interrupt()方法仅仅是在当前线程中打了一个停止的标记,并不是真正的停止线程

package org.fool.java.concurrent.interrupt;

public class InterruptTest1 {

    public static void main(String[] args) {
        try {
            Thread thread = new Thread(new MyThread());
            thread.start();
            Thread.sleep(5000);
            thread.interrupt();
        } catch (InterruptedException e) {
            System.out.println("main catch");
            e.printStackTrace();
        }
    }

    public static class MyThread implements Runnable {
        @Override
        public void run() {
            for (int i = 0; i < 200000; i++) {
                System.out.println("i = " + (i + 1));
            }
        }
    }
}

Console Output


Note:

从运行结果来看,调用interrupt方法并没有停止线程

 

interrupted()

测试当前线程是否已经中断

package org.fool.java.concurrent.interrupt;

public class InterruptTest2 {

    public static void main(String[] args) {
        try {
            Thread thread = new Thread(new MyThread());
            thread.start();
            Thread.sleep(1000);
            thread.interrupt();
            System.out.println("is interrupted 1: " + thread.interrupted());
            System.out.println("is interrupted 2: " + thread.interrupted());
        } catch (InterruptedException e) {
            System.out.println("main catch");
            e.printStackTrace();
        }
    }

    public static class MyThread implements Runnable {
        @Override
        public void run() {
            for (int i = 0; i < 50000; i++) {
                System.out.println("i = " + (i + 1));
            }
        }
    }
}

Console Output


Note:

从控制台打印的结果来看,线程并未停止,这也证明了interrupted()方法的解释:测试当前线程是否已经中断。本例中这个“当前线程”是main,它从未中断过,所以打印的结果是两个false 

 

修改Main函数

package org.fool.java.concurrent.interrupt;

public class InterruptTest3 {

    public static void main(String[] args) {
        try {
            Thread thread = new Thread(new MyThread());
            thread.start();
            Thread.sleep(1000);
            Thread.currentThread().interrupt();
            System.out.println(Thread.currentThread().getName());
            System.out.println("is interrupted 1: " + Thread.currentThread().interrupted());
            System.out.println("is interrupted 2: " + Thread.currentThread().interrupted());
        } catch (InterruptedException e) {
            System.out.println("main catch");
            e.printStackTrace();
        }
    }

    public static class MyThread implements Runnable {
        @Override
        public void run() {
            for (int i = 0; i < 50000; i++) {
                System.out.println("i = " + (i + 1));
            }
        }
    }
}



Console Output


Note:

控制台第一次打印的结果是true,第二次为false;Java Doc中给出的解释是:测试当前线程是否已经中断,线程的中断状态由该方法清除。即如果连续两次调用该方法,则第二次调用将返回false(在第一次调用已清除flag后以及第二次调用检查中断状态之前,当前线程再次中断的情况除外)

所以,interrupted()方法具有清除状态flag的功能 

 

isInterrupted()

测试线程是否已经中断

package org.fool.java.concurrent.interrupt;

public class InterruptTest4 {

    public static void main(String[] args) {
        try {
            Thread thread = new Thread(new MyThread());
            thread.start();
            Thread.sleep(1000);
            thread.interrupt();
            System.out.println("is interrupted 1: " + thread.isInterrupted());
            System.out.println("is interrupted 2: " + thread.isInterrupted());
        } catch (InterruptedException e) {
            System.out.println("main catch");
            e.printStackTrace();
        }
    }

    public static class MyThread implements Runnable {
        @Override
        public void run() {
            for (int i = 0; i < 200000; i++) {
                System.out.println("i = " + (i + 1));
            }
        }
    }
}

Console Output


is interrupted 1: true 


is interrupted 2: true


Note:

isInterrupted()并未清除状态flag,所以打印两个true

 

Summary

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

isInterrupted(): 测试线程Thread对象是否已经是中断状态,但不清除中断状态flag 

 

Reference

Java多线程编程核心技术 高洪岩 注

 

 

  • 大小: 47.1 KB
  • 大小: 18.5 KB
  • 大小: 16.4 KB
  • 大小: 16.6 KB
  • 大小: 15.7 KB
  • 大小: 5.3 KB
  • 大小: 5.7 KB
分享到:
评论

相关推荐

    JAVA多线程之中断机制stop()、interrupted()、isInterrupted()

    主要介绍了JAVA多线程之中断机制stop()、interrupted()、isInterrupted()的相关资料,需要的朋友可以参考下

    Java多线程教程吐血整理干货.md

    interrupt,interrupted,isInterrupted方法区别 join方法 yield方法 多线程 进程和线程 进程与线程最主要的区别是它们是操作系统管理资源的不同方式的体现。 准确来说进程与线程属于衍生关系。 进程是操作系统执行...

    Thread类的interrupt(),interrupted(),isInterrupted()1

    一般来说,支持线程中断的方法(也就是线程中断后会抛出InterruptedException的方法),阻塞函数,如:Thread.sleep、Thread.jo

    java线程中的interrupt,isInterrupt,interrupted方法

    在 Java 中,线程(Thread)类提供了三个相关的方法:interrupt、isInterrupted 和 interrupted,这三个方法都是用于处理线程的中断状态的。下面我们将详细介绍这三个方法的用法和区别。 interrupt 方法 interrupt...

    线程中断的方法以及静态方法isInterrupted和实例方法interrupted的区别

    线程中断 常见的有以下两种方式: 通过共享的标记来进行沟通 调用 interrupt() 方法来通知 通过共享的标记来实现中断 就是创建一个boolean类型的变量来控制循环是否进行,就是一个标记。 代码如下: ...

    java8源码-JavaSE-Code:JavaSE的代码练习与学习笔记总结

    interrupted与isInterrupted的区别: interrupted是Thread类的静态方法,里面调用了isTnterrupted方法[currentThread().isInterrupted()],测试当前线程是否已经中断,线程的中断状态由该方法清除 isInterrupted是Thread...

    Java中interrupt的使用.docx

    中断在java中主要有3个方法,interrupt(),isInterrupted()和interrupted()。 interrupt(),在一个线程中调用另一个线程的interrupt()方法,即会向那个线程发出信号——线程中断状态已被设置。至于那个线程何去何从...

    JUC学习.docx Java

    (4)start和run:不能直接调用run方法,因为这样并不会启动一个新的线程,依旧是在main线程来执行。相当于普通方法的直接调用 2.常见方法: (1)Sleep:写的位置决定作用于哪个线程,其他线程可以通过interrupt来打断...

    Java期末复习||应用程序设计-多线程和泛型

    线程操作:isAlive()、isInterrupted()、join()、sleep()、stop()、interrupted()、setDaemon()、setPriority()、yield() 同步与死锁、Object类对线程的支持 泛型、通配符、受限泛型、泛型接口、泛型方法、泛型数字...

    处理 InterruptedException1

    中断状态可以通过 Thread.isInterrupted() 来读取,并且可以通过一个名为 Thread.interrupted() 的操作读取和清除。 中断是一种协作机制,当一个线程中断另一个线程时,被中断的线程不一定要立即停止正在做的事情。...

    Java中断一个线程操作示例

    主要介绍了Java中断一个线程操作,结合实例形式分析了java中断线程相关的interrupt()、isInterrupted()及interrupted()函数使用技巧,需要的朋友可以参考下

Global site tag (gtag.js) - Google Analytics