interrupt和interrupted和isInterrupted的区别
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11413917.html
interrupt

Code Demo
package org.fool.thread;
public class InterruptTest1 {
public static void main(String[] args) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 100000; i++) {
System.out.println(Thread.currentThread().getName() + " i = " + i);
}
}
});
thread.start();
thread.interrupt();
}
}
Note:
从运行结果来看,调用interrupt方法并没有停止线程
interrupted

Code Demo
package org.fool.thread;
public class InterruptTest2 {
public static void main(String[] args) {
Thread thread = new Thread(() -> {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + " i = " + i);
if (i == 5) {
Thread.currentThread().interrupt();
System.out.println("interrupted 1: " + Thread.interrupted());
System.out.println("interrupted 2: " + Thread.interrupted());
}
}
});
thread.start();
}
}
Console Output

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

Code Demo
package org.fool.thread;
public class InterruptTest3 {
public static void main(String[] args) {
Thread thread = new Thread(() -> {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + " i = " + i);
}
});
thread.start();
// main thread interrupt
Thread.currentThread().interrupt();
System.out.println(thread.getName() + ":" + thread.isInterrupted());
System.out.println(thread.getName() + ":" + thread.isInterrupted());
System.out.println(Thread.currentThread().getName() + ":" + Thread.currentThread().isInterrupted());
System.out.println(Thread.currentThread().getName() + ":" + Thread.currentThread().isInterrupted());
// thread interrupt
thread.interrupt();
System.out.println(thread.getName() + ":" + thread.isInterrupted());
System.out.println(thread.getName() + ":" + thread.isInterrupted());
}
}
Console Output

Summary
调用interrupt()方法仅仅是在当前线程中打了一个停止的标记,并不是真正的停止线程
interrupted()测试当前线程是否已经是中断状态,执行后具有清除中断状态flag的功能
isInterrupted()测试线程Thread对象是否已经是中断状态,但不清除中断状态flag
interrupt和interrupted和isInterrupted的区别的更多相关文章
- java中interrupt,interrupted和isInterrupted的区别
文章目录 isInterrupted interrupted interrupt java中interrupt,interrupted和isInterrupted的区别 前面的文章我们讲到了调用int ...
- interrupt(),interrupted() 和 isInterrupted() 的区别
1. 结论先行 interrupt():将调用该方法的对象所表示的线程标记一个停止标记,并不是真的停止该线程. interrupted():获取当前线程的中断状态,并且会清除线程的状态标记.是一个是静 ...
- java---interrupt、interrupted和isInterrupted的区别
1.interrupt() interrupt方法用于中断线程.调用该方法的线程的状态为将被置为"中断"状态. 注意:线程中断仅仅是置线程的中断状态位,不会停止线程.需要用户自己 ...
- 【JAVA多线程】interrupted() 和 isInterrupted() 的区别
Thread 类中提供了两种方法用来判断线程的状态是不是停止的.就是我们今天的两位主人公 interrupted() 和 isInterrupted() . interrupted() 官方解释:测试 ...
- java线程interrupt、interrupted 、isInterrupted区别
前言 在分析interrupt之前,应该先了解java里线程有5种状态,其中有一个阻塞状态,interrupt和阻塞有关. interrupt() 方法 作用于要中断的那个线程. interrupt( ...
- java 多线程5: java 终止线程及中断机制 (stop()、interrupt() 、interrupted()、isInterrupted())
JAVA中有3种方式可以终止正在运行的线程 ①线程正常退出,即run()方法执行完毕了 ②使用Thread类中的stop()方法强行终止线程.但stop()方法已经过期了,不推荐使用 ③使用中断机制i ...
- 关于Java多线程-interrupt()、interrupted()、isInterrupted()解释
多线程先明白一个术语"中断状态",中断状态为true,线程中断. interrupt():就是通知中止线程的,使"中断状态"为true. isInterrupt ...
- interrupt ,interrupted 和 isInterrupted
1.interrupt interrupt方法用于中断线程.调用该方法的线程的状态为将被置为"中断"状态. 注意:线程中断仅仅是置线程的中断状态位,不会停止线程.需要用户自己去监 ...
- JAVA多线程之中断机制(stop()、interrupted()、isInterrupted())
一,介绍 本文记录JAVA多线程中的中断机制的一些知识点.主要是stop方法.interrupted()与isInterrupted()方法的区别,并从源代码的实现上进行简单分析. JAVA中有3种方 ...
随机推荐
- .Net Core 使用Redis进行数据缓存
1.运行环境 开发工具:Visual Studio 2017 JDK版本:.NET Core 2.0 项目管理工具:nuget 2.GITHUB地址 https://github.com/nbfujx ...
- PHP curl_file_create函数
curl_file_create — 创建一个 CURLFile 对象. 说明 CURLFile curl_file_create ( string $filename [, string $mime ...
- PHP curl_escape函数
curl_escape — 对给定的字符串进行URL编码. 说明 string curl_escape ( resource $ch , string $str ) 该函数对给定的字符串进行URL编码 ...
- VMware linux 克隆机的配置
从另一台虚拟机克隆完后的一些配置 编辑eth0的配置文件: [root@wen data01:4]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 删除 ...
- Ant Design Pro (中后台系统)教程
一.概念:https://pro.ant.design/docs/getting-started-cn(官方网站) 1.Ant Design Pro 是什么: https://www.cnblogs ...
- linux记事工具:RedNotebook Lifeograph Kontact ThotKeeper
Linux桌面有许多灵活而功能强大的日记工具,如支持标签.加密.多种日志模版和实时搜索.其中的优秀者包括: RedNotebook Lifeograph Kontact ThotKeeper
- selenium2-java 浏览器cookie的获取
//成功登陆后增加如下代码 File cookieFile = new File("C:\\tmp\\tangdai.cookie.txt"); ...
- docker 实战
创建镜像 docker pull ubuntu 创建容器 docker run -it -name web ubuntu /bin/bash 更新软件源信息 apt-get update 安装ssh ...
- SQL语句优化方式
不要使用*号进行查询操作,使用具体字段. 索引 在where子句和order by 涉及的字段上合理的添加索引. where 子句优化 避免在where子句中对null值进行判断,应对字段设置默认值 ...
- Codeforces 1114D(区间DP)
题面 传送门 分析 法1(区间DP): 首先,我们可以把连续的相等区间缩成一个数,用unique来实现,不影响结果 {1,2,2,3,3,3,5,3,4}->{1,2,3,5,3,4} 先从一个 ...