【JAVA多线程】interrupted() 和 isInterrupted() 的区别
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 对象 是否已经是中断状态,但不清楚状态标志。
【JAVA多线程】interrupted() 和 isInterrupted() 的区别的更多相关文章
- java中interrupt,interrupted和isInterrupted的区别
文章目录 isInterrupted interrupted interrupt java中interrupt,interrupted和isInterrupted的区别 前面的文章我们讲到了调用int ...
- java---interrupt、interrupted和isInterrupted的区别
1.interrupt() interrupt方法用于中断线程.调用该方法的线程的状态为将被置为"中断"状态. 注意:线程中断仅仅是置线程的中断状态位,不会停止线程.需要用户自己 ...
- Java多线程sleep和wait的区别,总结得非常好。
我们都知道sleep是让线程休眠,到时间后会继续执行,wait是等待,需要唤醒再继续执行,那么这两种方法在多线程中的表现形态,它们各有什么区别呢? 可以总结为以下几点. 使用上 从使用角度看,slee ...
- interrupt(),interrupted() 和 isInterrupted() 的区别
1. 结论先行 interrupt():将调用该方法的对象所表示的线程标记一个停止标记,并不是真的停止该线程. interrupted():获取当前线程的中断状态,并且会清除线程的状态标记.是一个是静 ...
- Java 中 interrupted 和 isInterrupted 方法的区别?
interrupt interrupt 方法用于中断线程.调用该方法的线程的状态为将被置为"中断"状态. 注意:线程中断仅仅是置线程的中断状态位,不会停止线程.需要用户自己去监 视 ...
- java多线程 sleep()和wait()的区别
接触了一些多线程的东西,还是从java入手吧. 相信看这篇文章的朋友都已经知道进程和线程的区别,也都知道了为什么要使用多线程了. 这两个方法主要来源是,sleep用于线程控制,而wait用于线程间的通 ...
- interrupt和interrupted和isInterrupted的区别
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11413917.html interrupt Code Demo package org.fool.th ...
- Java多线程通讯---------wait,notify区别
class Res{ public String username; public String sex; } class Out extends Thread{ Res res; public Ou ...
- java 多线程Thread和Runnable的区别
如果一个类继承Thread,则不适合资源共享.但是如果实现了Runable接口的话,则很容易的实现资源共享 实现Runnable接口比继承Thread类所具有的优势:1. 适合多个相同的程序代码的线程 ...
随机推荐
- 手机计算器1+1=2---Appium自动化
要想计算1+1=2,首先要定位到按钮1,定位方式和selenium类似
- centos7:mysql-5.7.23安装(二进制安装)
mysql有二进制码安装,和源码编译安装(mysql5.5使用cmake安装,mysql5.7需要安装boost依赖安装),因为boost依赖安装麻烦,所以用二进制码安装 MySql 5.7.23安装 ...
- Count Up Down(上下计数)
这个题目是 Kayak 发布的代码挑战题目. 最简单的描述就是不使用循环,输出 0 到 5,然后同样不是会用循环的方式再次输出 5 到 0. 英文描述 Part 1 Write a program t ...
- 轮播,试用与微信公众号,apicloud苹果安卓
<head> <script type="text/javascript" src="script/jquery.min.js">< ...
- 阻止ajax缓存方法
通过添加meta标签 <meta http-equiv= "pragma" content= "no-cache"/> (pragma: 杂注) & ...
- 解决无法安装Microsoft .Net Framework 3.5
如果解决不了,试试我的方法吧,我也在网上找了好久,最终在本地解决了 所需工具:dism,和Net Framework 3.5,已经打包 链接:https://pan.baidu.com/s/1nKok ...
- 『MXNet』第五弹_MXNet.image图像处理
简单处理API 读取图像: image.imdecode(open('../img/cat1.jpg', 'rb').read()) 图像类型转换: img.astype('float32') 图像增 ...
- logging addHandler(console)
import logging # set up logging to file - see previous section for more details logging.basicConfig( ...
- 5月21 练习AJAX的查看详细及批量删除
老师讲过之后的复习: 显示数据的代码部分: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &q ...
- 3G 4G 5G中的网络安全问题——文献汇总
Modeling and Analysis of RRC-Based Signalling Storms in 3G Networks 还是使用状态机模型来做恶意UE识别 https://san.ee ...