【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. 适合多个相同的程序代码的线程 ...
随机推荐
- Linux中apt与apt-get命令的区别与解释
2019-01-15 14:35:39 随着 apt install package 命令的使用频率和普遍性逐步超过 apt-get install package,越来越多的其它 Linux 发行版 ...
- 对nginx进行平滑升级
1.查看服务器当前nginx版本 [root@instance-hwl9ix5l licenses]# nginx -v #查看版本 nginx: nginx version: n ...
- mo系统常用语句
mo系统常用语句 一.总结 一句话总结: 1.语言:双语设置(繁体,英语)语句? {:chooseLanguage("確定要刪除么","Are you sure you ...
- 数据库:Mysql中“select ... for update”排他锁分析
Mysql InnoDB 排他锁 用法: select … for update; 例如:select * from goods where id = 1 for update; 排他锁的申请前提:没 ...
- RESTful Web Services中API的设计原则(转)
当下前后端分离的设计已经是web app开发的标配,但是如何设计一个强壮,扩展性好,又规范的API呢 参考以下link,可以得到需要有益的启示.同时个人推荐一本书<web API的设计和开发&g ...
- vs2015多行注释与取消多行注释
注释: 先CTRL+K,然后CTRL+C 取消注释: 先CTRL+K,然后CTRL+U
- v891 安卓平板的root
看了 油管的视频,标题:Onda V891 - Root Android 4.4.4 (Kit Kat) 2)下载工具,照着做就行了. 3)root完之后,安装 [钛备份](TitaniumBacku ...
- LeetCode--383--赎金信
问题描述: 给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串ransom能不能由第二个字符串magazines里面的字符构成.如果可以构成,返回 true ...
- laravel App\Kernel.php中的middleware、middlewareGroups、routeMiddleware
万事万物总逃不出一个理字,程序尤其如此,你之所以活得轻松,是因为有人替你负重前行,帮你屏蔽掉了很多乱七八糟的事情,但总有一天你要直面这些事情.程序亦是如此,某个框架你用的很轻松,那是因为底层逻辑已经有 ...
- canal入门Demo
关于canal具体的原理,以及应用场景,可以参考开发文档:https://github.com/alibaba/canal 下面给出canal的入门Demo (一)部署canal服务器 可以参考官方文 ...