【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. 适合多个相同的程序代码的线程 ...
随机推荐
- XLua与CSharp交互的采坑点 : 热修复返回值为 Int 的CSharp方法
1.假如CS的一个类中有如下逻辑: using System.Collections; using System.Collections.Generic; using UnityEngine; usi ...
- Nginx的安装和使用(Linux)
关于什么是Nginx,Nginx的优势和使用范围这里就不多说了.其实它就是一个web服务器.这篇文章主要是说Nginx的安装和使用. 安装方式有yum安装和源码安装,这里主要讲源码安装 1.安装依赖, ...
- nodejs通过request请求远程url的文件并下载到本地
需要循环去下载远程文件,然后自己写了一个demo,可以直接运行,如下: //文件下载 var fs = require("fs"); var path = require(&quo ...
- jquery ----> How to Create a Basic Plugin (翻译)
http://learn.jquery.com/plugins/basic-plugin-creation/ 如何创建一个基本的插件 有时候你想在整个代码中提供一些功能. 例如,也许你想要一个单一的方 ...
- linux常用命令及系统常见符号
常用命令 1.start x 进入界面 2.shutdown -h now 立刻关机 shutdown -r now 立刻重新启动 reboot 立刻重新启动 3.su root 切换成超级管理员 4 ...
- IntelliJ IDEA的这些配置,你值得拥有
一.自动编译开关 二.忽略大小写开关 IDEA默认是匹配大小写,此开关如果未关.你输入字符一定要符合大小写.比如你敲string是不会出现代码提示或智能补充.但是,如果你开了这个开关,你无论输入Str ...
- 矩阵最优路线DP
母题:矩阵中每个点有权值,每经过一个点就累加权值,求从a点到b点的最优(最大)路线. 题型1: 从左上到右下,只能向下或者向右 for 行 for 列 dp=max dp左,dp上; 扫一遍就行 有时 ...
- PHP用PDO
$pdo = new PDO('mysql:host=localhost;dbname=jmyp','root','root'); $pdo->exec('set names utf8'); $ ...
- Arthur and Brackets CodeForces - 508E (贪心,括号匹配)
大意: n个括号, 位置未知, 给出每对括号左右间距, 求输出一个合法括号序列. 最后一个括号间距一定为1, 从右往左遍历, 每次括号划分越小越好. #include <iostream> ...
- Android测试(二)——drozer使用
drozer启动: 1)首先在模拟 器或者安卓设备上开启drozer; 2)然后打开adb,转发端口: adb forward tcp:31415 tcp:31415 3)在电脑上开启drozer: ...