当前位置:网站首页>线程中断
线程中断
2022-06-09 17:57:00 【西柚dzh】
线程中断
线程中断即线程运行过程中被其他线程给打断了,它与 stop 最大的区别是:stop 是由系统强制终止线程,而线程中断则是给目标线程发送一个中断信号 如果目标线程没有接收线程中断的信号并结束线程,线程则不会终止,具体是否退出或者执行其他逻辑由目标线程决定
例1 中断失败
package com.starry.codeview.threads.P05_ThreadInterrupt;
/**
* 线程中断失败, 因为目标线程收到中断信号并没有做出处理
*/
public class T01_ThreadInterrupt_Failed {
static int i = 10;
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
while (i++ < 56) {
System.out.println("t1线程执行");
Thread.yield();
}
});
t1.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t1.interrupt();
}
}例2 中断成功
package com.starry.codeview.threads.P05_ThreadInterrupt;
/**
* 线程中断成功, 目标线程收到中断信号做出了处理
*/
public class T02_ThreadInterrupt_Successed {
static int i = 10;
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
while (i++ < 56) {
System.out.println("t1线程执行");
Thread.yield();
if (Thread.currentThread().isInterrupted()) {
System.out.println("t1线程被中断,退出");
return;
}
}
});
t1.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t1.interrupt();
}
}例3 Sleep中断失败
package com.starry.codeview.threads.P05_ThreadInterrupt;
/**
* 线程中断失败,Sleep遇到线程中断catch到异常会清除掉中断标记,所以线程会继续循环
*/
public class T03_ThreadInterrupt_Sleep_Failed {
static int i = 10;
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
while (i++ < 56) {
System.out.println("t1线程执行");
Thread.yield();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
System.out.println("t1线程sleep被中断");
e.printStackTrace();
}
}
});
t1.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t1.interrupt();
}
}例4 Sleep中断成功
package com.starry.codeview.threads.P05_ThreadInterrupt;
/**
* 线程中断失败,Sleep遇到线程中断catch到异常会清除掉中断标记,但是catch异常块中做出了中断处理动作,所以中断成功!!!
*/
public class T04_ThreadInterrupt_Sleep_Successed {
static int i = 10;
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
while (i++ < 56) {
System.out.println("t1线程执行");
Thread.yield();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("t1线程sleep被中断");
Thread.currentThread().interrupt();
}
}
});
t1.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t1.interrupt();
}
}版权属于:dingzhenhua 本文链接:https://www.dcmickey.cn/Java/221.html 转载时须注明出处及本声明
边栏推荐
- Abbexa AEC 色原试剂盒使用说明
- Tencent cloud database tdsql | in fact, we experience such college entrance examinations every day
- 一些有趣的B+树优化实验
- [notes of advanced mathematics] Green formula, Gauss formula, Stokes formula, field theory
- NLP- 关键词提取 - 综述
- NLP keyword extraction overview
- NLP - 关键词提取 - TextRank
- 文心 ERNIE 3.0加持!小样本也可实现全量数据99%的效果!
- Development and practice of the martyr's family search system
- Snap宣布升级相机产品和AR生态 将持续深耕中国市场
猜你喜欢
Android caching mechanism lrucache

NLP - 关键词提取 - TextRank
![[work with notes] multiple coexistence of ADB, sound card, network card and serial port of Tina system](/img/c9/e57db5cbcd0599717f551c591a86a5.png)
[work with notes] multiple coexistence of ADB, sound card, network card and serial port of Tina system

智充推出NET ZERO SERIES储能充一体机,携手比亚迪共创净零未来

ZigBee networking has never been so simple!

Epigentek hi fi cDNA synthesis kit instructions

如何实现自定义富文本编辑器标签
MySQL 8.0.29 解压版安装配置方法图文教程

Moco -Momentum Contrast for Unsupervised Visual Representation Learning

redis源码学习-01_Clion中调试redis源码
随机推荐
Welcome to the InfoQ writing platform!
Abbexa 质粒 MiniPrep 试剂盒检测程序
Word使用技巧
Walkthrough - combined use of grouping and aggregation functions
刷脸认证如何实现人脸又快又准完成校验?
10分钟快速入门RDS【华为云至简致远】
Function throttling for performance optimization in JS
Ali's 10-year Technician: seven ways of thinking of the leader
Macro definition CV with parameters in opencv_ Role of assert()
DM8查看SQL执行计划的5种方法(测试+调优用)
NLP- 关键词提取 - 综述
华为云零代码开发图片压缩工具
[data processing] pandas reads SQL data
c語言解决爬樓梯問題
空闲内存的管理
NLP keyword extraction overview
Imshow() of OpenCV to view the pixel value of the picture
【嵌入式工程师·单片机】① 基础概念 之 单片机
nlp网络中两种残差结构对网络的影响
【长时间序列预测】Aotoformer 代码详解之[1]数据预处理及数据读取