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

interrupted()

官方解释:测试当前线程是否已经中断,当前线程是指运行 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

程序运行结束后,结果如上图所示,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

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

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


isInterrupted()

从声明中可以看出来 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


总结

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

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

转载于:https://www.cnblogs.com/forfreewill/p/9111162.html

【JAVA多线程】interrupted() 和 isInterrupted() 的区别相关推荐

  1. java中interrupt,interrupted和isInterrupted的区别

    文章目录 isInterrupted interrupted interrupt java中interrupt,interrupted和isInterrupted的区别 前面的文章我们讲到了调用int ...

  2. JAVA interrupt、interrupted和isInterrupted的区别

    提前总结: interrupt() 向当前调用者线程发出中断信号 isinterrupted() 查看当前中断信号是true还是false interrupted() 是静态方法,查看当前中断信号是t ...

  3. 【Java】interrupt、interrupted和isInterrupted的区别

    今天在看到Thread类的isInterrupted方法可以获取线程的中断状态: 于是写了个例子想验证一下: public class Interrupt {public static void ma ...

  4. 简述Thread的interrupt()、interrupted()及isInterrupted()的区别

    前言 在java Thread类中,我们会看到interrupt().interrupted()及isInterrupted(),在大多数情况下,我们都不会使用到它们,但是有一个Interrupted ...

  5. Java多线程sleep和wait的区别,总结得非常好

    转载自 Java多线程sleep和wait的区别,总结得非常好 我们都知道sleep是让线程休眠,到时间后会继续执行,wait是等待,需要唤醒再继续执行,那么这两种方法在多线程中的表现形态,它们各有什 ...

  6. 多线程之 interrupt,interrupted,isInterrupted 方法区别

    首先看测试代码 /** * Created by Jarvis.y on 2020/11/5 * <p> * interrupt , interrupted , isInterrupted ...

  7. 【Java之多线程(二)】(***重要***)Java多线程中常见方法的区别,如object.wait()和Thread.sleep()的区别等

    1.Java中Thread和Runnable的区别??? 区别: 在程序开发中只要是多线程肯定永远以实现Runnable接口为主,因为实现Runnable接口相比继承Thread类有如下好处: 避免点 ...

  8. java多线程 sleep()和wait()的区别

    此文章来自"Intel Software"应用开发 接触了一些多线程的东西,还是从java入手吧. 相信看这篇文章的朋友都已经知道进程和线程的区别,也都知道了为什么要使用多线程了. ...

  9. linux线程wait和sleep,java多线程 sleep()和wait()的区别

    接触了一些多线程的东西,还是从java入手吧. 相信看这篇文章的朋友都已经知道进程和线程的区别,也都知道了为什么要使用多线程了. 这两个方法主要来源是,sleep用于线程控制,而wait用于线程间的通 ...

最新文章

  1. 如何按PHP中给定键的值对关联数组进行排序?
  2. L0,L1,L2范数
  3. 释疑の手工凭证界面不显示页数
  4. 成功的九字真言(冯唐)
  5. ios numlock_从“提示”框:默认情况下启用NumLock,无广告的iOS应用和立体声供电的派对灯...
  6. 马尔科夫随机场之图像去燥【Matlab实现,PRML例子】
  7. 《学习OpenCV》课后习题解答(第四章)(仅供参考)(不断更新)
  8. Hadoop MapReduce编程 API入门系列之join(二十六)
  9. Microsoft Office Mobile 2010 Beta 于 4 月 5 日过期
  10. 在苹果Mac上找不到文件存储位置怎么办?
  11. 期末复习概率论与数理统计时遇到的那些证明题
  12. C语言加减乘除运算符
  13. IE浏览器打开github点击code无响应
  14. 深入分析用 Manimgl 绘制参数方程的图像
  15. 2018秦皇岛ccpc赛后总结
  16. linux认证在哪考试,2017年Linux认证考试练习题
  17. 广义线性混合模型(GLMM)变量选择
  18. Lidar_imu自动标定源码阅读(二)——calibration部分
  19. ESP8266远程控制LED
  20. 牛客网--关于合法括号序列判断

热门文章

  1. Pytorch还是TensorFlow?顶会带你览趋势
  2. 回顾 | AAAI 2019最佳论文公布,CMU、斯坦福、MIT上榜
  3. 阿里达摩院发布2019十大科技趋势!AI专用芯片将挑战GPU的绝对统治地位
  4. mAP(mean Average Precision)应用(转)
  5. 云服务器怎么把软件装上去_服务器:云服务器怎么设置301
  6. .net core json 为null输出_SpringBoot实战(九):标准化json返回值
  7. 「雕爷学编程」Arduino动手做(24)——水位传感器模块
  8. 自己动手写操作系统(二)一个最小的“操作系统”
  9. 使用Jenkins来实现内部的持续集成流程(上)
  10. selenium-python:运行后报浏览器不兼容 disconnected: unable to connect to renderer