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种方 ...
随机推荐
- Vue.js----date与时间戳的转换(unixTime)Moment.js让日期处理变得更简单
当前日期格式化 let curTime = moment().format('YYYY-MM-DD HH:mm:ss') console.log('当前日期时间curTime:' + curTime) ...
- BestCoder Round #92 (hdu 6015 6016)
比赛链接 A题主要是map的使用,比赛的时候问了下队友,下次要记住了 #include<bits/stdc++.h> using namespace std; typedef long l ...
- UVA 12012 Detection of Extraterrestrial(KMP求循环节)
题目描述 E.T. Inc. employs Maryanna as alien signal researcher. To identify possible alien signals and b ...
- 540D - Bad Luck Island(概率DP)
原题链接:http://codeforces.com/problemset/problem/540/D 题意:给你石头.剪刀.布的数量,它们之间的石头能干掉剪刀,剪刀能干掉布,布能干掉石头,问最后石头 ...
- <HTTP权威指南>记录 ---- 网络爬虫
网络爬虫 网络爬虫(web crawler)能够在无需人类干预的情况下自动进行一系列Web事务处理的软件程序.很多爬虫会从一个Web站点逛到另一个Web站点,获取内容,跟踪超链,并对它们找到的数据进行 ...
- 【玩转SpringBoot】异步任务执行与其线程池配置
同步代码写起来简单,但就是怕遇到耗时操作,会影响效率和吞吐量. 此时异步代码才是王者,但涉及多线程和线程池,以及异步结果的获取,写起来颇为麻烦. 不过在遇到SpringBoot异步任务时,这个问题就不 ...
- linux主机hang住echo 0 > /proc/sys/kernel/hung_task_timeout_secs disables this message
用dmesg显示如下图信息 问题原因: 默认情况下, Linux会最多使用40%的可用内存作为文件系统缓存.当超过这个阈值后,文件系统会把将缓存中的内存全部写入磁盘, 导致后续的IO请求都是同步的. ...
- 初识Flink-从WorldCount开始
Apache Flink是一个用于分布式流和批处理数据处理的开源平台.Flink的核心是流数据流引擎,为数据流上的分布式计算提供数据分发,通信和容错.Flink在流引擎之上构建批处理,覆盖本机迭代支持 ...
- redis集群搭建(简单简单)一台机器多redis
redis集群搭建 在开始redis集群搭建之前,我们先简单回顾一下redis单机版的搭建过程 下载redis压缩包,然后解压压缩文件: 进入到解压缩后的redis文件目录(此时可以看到Makef ...
- python3_OS模块
一.什么是os模块 os模块提供了多数操作系统的功能接口函数.当os模块被导入后,它会自适应于不同的操作系统平台,根据不同的平台进行相应的操作,在python编程时,经常和文件.目录打交道,所以离不了 ...