当前位置: 首页 > news >正文

中断机制-interrupt和isInterrupted源码分析、中断协商案例

当前线程的中断标识为true,是不是线程就立刻停止?

答案是不立刻停止,具体来说,当对一个线程,调用interrupt时:

  • 如果线程处于正常活动状态,那么会将该线程的中断标志设置为true,仅此而已,被设置中断标志的线程将继续正常运行,不受影响,所以interrupt()并不能真正的中断线程,需要被调用的线程自己进行配合才行,对于不活动的线程没有任何影响。

  • 如果线程处于阻塞状态(例如sleep,wait,join状态等),在别的线程中调用当前线程对象的interrupt方法,那么线程将立即退出被阻塞状态(interrupt状态也将被清除),并抛出一个InterruptedException异常。

第一种情况正常活动状态演示 

package com.nanjing.gulimall.zhouyimo.test;import java.util.concurrent.TimeUnit;/*** @author zhou* @version 1.0* @date 2023/10/15 5:43 下午* 执行interrupt方法将t1标志位设置为true后,t1没有中断,仍然完成了任务后再结束* 在2000毫秒后,t1已经结束称为不活动线程,设置状态为没有任何影响*/
public class InterruptDemo4 {public static void main(String[] args) {//实例方法interrupt()仅仅是设置线程的中断状态位为true,不会停止线程Thread t1 = new Thread(() -> {for (int i = 1; i <= 300; i++) {System.out.println("------: " + i);}/*** ------: 298* ------: 299* ------: 300* t1线程调用interrupt()后的中断标志位02:true*/System.out.println("t1线程调用interrupt()后的中断标志位02:" + Thread.currentThread().isInterrupted());}, "t1");t1.start();System.out.println("t1线程默认的中断标志位:" + t1.isInterrupted());//falsetry {TimeUnit.MILLISECONDS.sleep(2);} catch (InterruptedException e) {e.printStackTrace();}t1.interrupt();//true/*** ------: 251* ------: 252* ------: 253* t1线程调用interrupt()后的中断标志位01:true*/System.out.println("t1线程调用interrupt()后的中断标志位01:" + t1.isInterrupted());//truetry {TimeUnit.MILLISECONDS.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}//2000毫秒后,t1线程已经不活动了,不会产生任何影响System.out.println("t1线程调用interrupt()后的中断标志位03:" + t1.isInterrupted());//false}
}
t1线程默认的中断标志位:false
------: 1
------: 2
------: 3
------: 4
------: 5
------: 6
------: 7
------: 8
------: 9
------: 10
------: 11
------: 12
------: 13
------: 14
------: 15
------: 16
------: 17
------: 18
------: 19
------: 20
------: 21
------: 22
------: 23
------: 24
------: 25
------: 26
------: 27
------: 28
------: 29
------: 30
------: 31
------: 32
------: 33
------: 34
------: 35
------: 36
------: 37
------: 38
------: 39
------: 40
------: 41
------: 42
------: 43
------: 44
------: 45
------: 46
------: 47
------: 48
------: 49
------: 50
------: 51
------: 52
------: 53
------: 54
------: 55
------: 56
------: 57
------: 58
------: 59
------: 60
------: 61
------: 62
------: 63
------: 64
------: 65
------: 66
------: 67
------: 68
------: 69
------: 70
------: 71
------: 72
------: 73
------: 74
------: 75
------: 76
------: 77
------: 78
------: 79
------: 80
------: 81
------: 82
------: 83
------: 84
------: 85
------: 86
------: 87
------: 88
------: 89
------: 90
------: 91
------: 92
------: 93
------: 94
------: 95
------: 96
------: 97
------: 98
------: 99
------: 100
------: 101
------: 102
------: 103
------: 104
------: 105
------: 106
------: 107
------: 108
------: 109
------: 110
------: 111
------: 112
------: 113
------: 114
------: 115
------: 116
------: 117
------: 118
------: 119
------: 120
------: 121
------: 122
------: 123
------: 124
------: 125
------: 126
------: 127
------: 128
------: 129
------: 130
------: 131
------: 132
------: 133
------: 134
------: 135
------: 136
------: 137
------: 138
------: 139
------: 140
------: 141
------: 142
------: 143
------: 144
------: 145
------: 146
------: 147
------: 148
------: 149
------: 150
------: 151
------: 152
------: 153
------: 154
------: 155
------: 156
------: 157
------: 158
------: 159
------: 160
------: 161
------: 162
------: 163
------: 164
------: 165
------: 166
------: 167
------: 168
------: 169
------: 170
------: 171
------: 172
------: 173
------: 174
------: 175
------: 176
------: 177
------: 178
------: 179
------: 180
------: 181
------: 182
------: 183
------: 184
------: 185
------: 186
------: 187
------: 188
------: 189
------: 190
------: 191
------: 192
------: 193
------: 194
------: 195
------: 196
------: 197
------: 198
------: 199
------: 200
------: 201
------: 202
------: 203
------: 204
------: 205
------: 206
------: 207
------: 208
------: 209
------: 210
------: 211
------: 212
------: 213
------: 214
------: 215
------: 216
------: 217
------: 218
------: 219
------: 220
------: 221
------: 222
------: 223
------: 224
------: 225
------: 226
------: 227
------: 228
------: 229
------: 230
------: 231
------: 232
t1线程调用interrupt()后的中断标志位01:true
------: 233
------: 234
------: 235
------: 236
------: 237
------: 238
------: 239
------: 240
------: 241
------: 242
------: 243
------: 244
------: 245
------: 246
------: 247
------: 248
------: 249
------: 250
------: 251
------: 252
------: 253
------: 254
------: 255
------: 256
------: 257
------: 258
------: 259
------: 260
------: 261
------: 262
------: 263
------: 264
------: 265
------: 266
------: 267
------: 268
------: 269
------: 270
------: 271
------: 272
------: 273
------: 274
------: 275
------: 276
------: 277
------: 278
------: 279
------: 280
------: 281
------: 282
------: 283
------: 284
------: 285
------: 286
------: 287
------: 288
------: 289
------: 290
------: 291
------: 292
------: 293
------: 294
------: 295
------: 296
------: 297
------: 298
------: 299
------: 300
t1线程调用interrupt()后的中断标志位02:true
t1线程调用interrupt()后的中断标志位03:falseProcess finished with exit code 0

第二种情况线程处于阻塞状态演示

package com.nanjing.gulimall.zhouyimo.test;import java.util.concurrent.TimeUnit;/*** @author zhou* @version 1.0* @date 2023/10/15 9:40 下午*///1.中断标志位默认为false
//2.t2对t1发出中断协商  t1.interrupt();
//3.中断标志位为true: 正常情况 程序停止//中断标志位为true  异常情况,.InterruptedException ,将会把中断状态清除,中断标志位为false
//4.需要在catch块中,再次调用interrupt()方法将中断标志位设置为false;
public class InterruptDemo5 {public static void main(String[] args) {Thread t1 = new Thread(() -> {while (true) {if (Thread.currentThread().isInterrupted()) {System.out.println(Thread.currentThread().getName() + " 中断标志位为:" + Thread.currentThread().isInterrupted() + " 程序停止");break;}//sleep方法抛出InterruptedException后,中断标识也被清空置为false,如果没有在//catch方法中调用interrupt方法再次将中断标识置为true,这将导致无限循环了try {Thread.sleep(200);} catch (InterruptedException e) {Thread.currentThread().interrupt();e.printStackTrace();}System.out.println("-------------hello InterruptDemo5");}}, "t1");t1.start();try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}new Thread(() -> {t1.interrupt();}, "t2").start();}
}
-------------hello InterruptDemo5
-------------hello InterruptDemo5
-------------hello InterruptDemo5
-------------hello InterruptDemo5
java.lang.InterruptedException: sleep interruptedat java.lang.Thread.sleep(Native Method)at com.nanjing.gulimall.zhouyimo.test.InterruptDemo5.lambda$main$0(InterruptDemo5.java:27)at java.lang.Thread.run(Thread.java:750)
-------------hello InterruptDemo5
t1 中断标志位为:true 程序停止

详细解释:


静态方法Thread.interrupted(),谈谈你的理解?

package com.nanjing.gulimall.zhouyimo.test;/*** @author zhou* @version 1.0* @date 2023/10/15 10:17 下午*/
public class InterruptDemo6 {public static void main(String[] args) {/*** main	false* main	false* -----------1* -----------2* main	true* main	false*/System.out.println(Thread.currentThread().getName() + "\t" + Thread.interrupted());//falseSystem.out.println(Thread.currentThread().getName() + "\t" + Thread.interrupted());//falseSystem.out.println("-----------1");Thread.currentThread().interrupt();System.out.println("-----------2");System.out.println(Thread.currentThread().getName() + "\t" + Thread.interrupted());//trueSystem.out.println(Thread.currentThread().getName() + "\t" + Thread.interrupted());//false}
}main	false
main	false
-----------1
-----------2
main	true
main	false

如果调用interrupt方法再次将中断标识置为true:

package com.nanjing.gulimall.zhouyimo.test;/*** @author zhou* @version 1.0* @date 2023/10/15 10:17 下午*/
public class InterruptDemo6 {public static void main(String[] args) {/*** main	false* main	false* -----------1* -----------2* main	true* main	false*/System.out.println(Thread.currentThread().getName() + "\t" + Thread.interrupted());//falseSystem.out.println(Thread.currentThread().getName() + "\t" + Thread.interrupted());//falseSystem.out.println("-----------1");Thread.currentThread().interrupt();System.out.println("-----------2");System.out.println(Thread.currentThread().getName() + "\t" + Thread.interrupted());//true//如果调用interrupt方法再次将中断标识置为trueThread.currentThread().interrupt();System.out.println(Thread.currentThread().getName() + "\t" + Thread.interrupted());//false}
}main	false
main	false
-----------1
-----------2
main	true
main	true

对于静态方法Thread.interrupted()和实例方法isInterrupted()区别在于:

静态方法interrupted将会清除中断状态(传入的参数ClearInterrupted为true)

实例方法isInterrupted则不会(传入的参数ClearInterrupted为false) 

总结 

相关文章:

中断机制-interrupt和isInterrupted源码分析、中断协商案例

当前线程的中断标识为true&#xff0c;是不是线程就立刻停止&#xff1f; 答案是不立刻停止&#xff0c;具体来说&#xff0c;当对一个线程&#xff0c;调用interrupt时&#xff1a; 如果线程处于正常活动状态&#xff0c;那么会将该线程的中断标志设置为true&#xff0c;仅此…...

我与COSCon的故事【时光的故事】

曾经 2019年的时候&#xff0c;我还在日本读研究生&#xff0c;做一些物联网 (Internet of Things, IoT) 网络中的底层P2P (Peer to Peer) 通讯仿真模拟。这个方向是新来的Nguyen老师的新方向&#xff0c;它跟计算机强相关&#xff0c;但是很小众&#xff0c;实验室里也没有前辈…...

【科学文献计量】利用pybibx分析Scopus文献数据集(EDA,N-Grams,Cluster,Network analysis,NLP)

利用pybibx分析Scopus文献数据集 1 运行前准备1.1 数据集1.2 前置库2 加载库3 数据导入4 探索式数据分析,即EDA4.1 表格可视化4.2 词云图可视化4.3 N-Grams可视化4.4 文献聚类4.5 主题词演化4.6 桑基图可视化4.7 树图可视化4.8 作者生产力可视化5 网络可视化5.1 文献引用与被引…...

-带你看懂11种API类型及应用-

一起走进多样的API&#xff0c;多样的精彩 随着互联网行业的日益发展&#xff0c;API(Application Programming Interface)这个名词对于绝大多数来说都已不再陌生。然而&#xff0c;实际上&#xff0c;根据不同标准可以划分出不同类型的API。今天&#xff0c;让我们来走…...

集成友盟qq互联分享,导出风险问题处理

处理方案&#xff1a;移除 android:exported"true"即可。 注意友盟SDK QQ share 里默认配置是android:exported"true"&#xff0c;所以要覆盖即可。...

探索数字安全的卓越之选 - Digicert证书

在数字时代&#xff0c;数据安全和隐私保护变得尤为重要。无论是个人网站、电子商务平台还是大型企业&#xff0c;保护用户数据和建立信任都是至关重要的任务。在这个领域&#xff0c;Digicert是一个备受推崇的品牌&#xff0c;提供了卓越的数字证书解决方案&#xff0c;以确保…...

第五章 流程控制 Pro

五、流程控制 1、条件语句 一、if语句&#xff08;三种形式&#xff09; 1、单分支语句: if &#xff08;表达式&#xff09;语句&#xff1b; //表达式可以是任何表达式 0和非0 多条语句加{ }构成复合语句 2、双分支语句 if(表达式) 语句1&#xff1b; else 语句2…...

CSS之实现线性渐变背景

1. background: linear-gradient() background: linear-gradient是CSS中用于创建线性渐变背景的属性&#xff0c;这个属性允许你定义一个在元素的背景中进行渐变的效果&#xff0c;可以从一个颜色过渡到另一个颜色。 基本语法 background: linear-gradient(direction, color-…...

软考 系统架构设计师系列知识点之特定领域软件体系结构DSSA(7)

接前一篇文章&#xff1a;软考 系统架构设计师系列知识点之特定领域软件体系结构DSSA&#xff08;6&#xff09; 所属章节&#xff1a; 第7章. 系统架构设计基础知识 第5节. 特定领域软件体系结构 相关试题 5. 特定领域软件架构&#xff08;Domain Specific Software Archite…...

CentOS-7网卡重启后关闭的解决方法

第一步查找网卡&#xff1a; ip addr 如下图&#xff1a; 于是发现网卡eth0。 第二步进入网卡配置目录并进行配置&#xff1a; cd /etc/sysconfig/network-scriptsvim ifcfg-eth0 第三步改配置如下图&#xff1a; 然后每次重启后网卡会自动启动。...

Linux CentOS7 用户组管理

Linux操作系统基于多用户的设计理念&#xff0c;允许多个用户同时使用系统资源。用户是登录系统并使用系统资源的个体&#xff0c;其都有自己的账户和密码。用户组是将众多用户归类为一组。Linux中的用户和用户组是系统安全和权限管理的基础。本文将探讨Linux中用户组的创建和管…...

C++算法:前缀和基础

相关 源码测试用例下载 https://download.csdn.net/download/he_zhidan/88430716 包括4个压缩包&#xff0c;初始代码&#xff0c;实现前缀和&#xff0c;实现前缀积&#xff0c;实现前缀异或。都是在前者的基础上修改的。 本博文是CSDN学院课程的讲义 https://edu.csdn.net/c…...

vue和react的区别

目录 1. 数据绑定 Vue React 2. 组件化 Vue React 3. 学习曲线 4. 状态管理 Vue React 5. 社区和生态系统 3. 学习曲线 4. 状态管理 Vue React 5. 生态系统 6. 社区和支持 7. 性能 8. 生产环境性能 9.语法和模板: 结论 当涉及到前端开发框架时&#xff0c…...

STM32 之 HAL 库串口 USART 丢数据及ORE卡死的解决方案

STM32 之 HAL 库串口 USART 丢数据及ORE卡死的解决方案_hal_uart_error_ore-CSDN博客...

递归最小二乘法RLS

参考&#xff1a;RLS递归最小二乘法(Recursive Least Squares)_hymwgk的博客-CSDN博客...

Apache Doris (三十九):Doris数据导出 - MySQL dump导出

🏡 个人主页:IT贫道_大数据OLAP体系技术栈,Apache Doris,Clickhouse 技术-CSDN博客 🚩 私聊博主:加入大数据技术讨论群聊,获取更多大数据资料。 🔔 博主个人B栈地址:豹哥教你大数据的个人空间-豹哥教你大数据个人主页-哔哩哔哩视频 目录...

【Nginx32】Nginx学习:随机索引、真实IP处理与来源处理模块

Nginx学习&#xff1a;随机索引、真实IP处理与来源处理模块 完成了代理这个大模块的学习&#xff0c;我们继续其它 Nginx 中 HTTP 相关的模块学习。今天的内容都比较简单&#xff0c;不过最后的来源处理非常有用&#xff0c;可以帮我们解决外链问题。另外两个其实大家了解一下就…...

vue3后台管理框架之集成sass

我们目前在组件内部已经可以使用scss样式,因为在配置styleLint工具的时候,项目当中已经安装过sass sass-loader,因此我们再组件内可以使用scss语法!!!需要加上lang="scss" <style scoped lang="scss"></style> 接下来我们为项目添加一些…...

无需付费开会员,一个Python程序实现PDF转高清图片

今天需要将一个PDF导出为图片&#xff0c;但是一般的在线转换网站导出的图片清晰度都不高&#xff0c;分辨率只有1241*1754&#xff0c;这就导致输出的图片放大后字体是有点模糊的&#xff0c;所以就想到了使用Python中的PyPDF2库来处理PDF文件&#xff0c;以及Pillow库来处理图…...

为分布式系统设计数据库

【squids.cn】 全网zui低价RDS&#xff0c;免费的迁移工具DBMotion、数据库备份工具DBTwin、SQL开发工具等 数据库设计是微服务和云原生解决方案的关键因素&#xff0c;因为基于微服务的架构导致了数据的分布式。数据管理不再在一个单一的过程中发生&#xff0c;而是可以通过多…...

React Native 开发环境搭建(全平台详解)

React Native 开发环境搭建&#xff08;全平台详解&#xff09; 在开始使用 React Native 开发移动应用之前&#xff0c;正确设置开发环境是至关重要的一步。本文将为你提供一份全面的指南&#xff0c;涵盖 macOS 和 Windows 平台的配置步骤&#xff0c;如何在 Android 和 iOS…...

循环冗余码校验CRC码 算法步骤+详细实例计算

通信过程&#xff1a;&#xff08;白话解释&#xff09; 我们将原始待发送的消息称为 M M M&#xff0c;依据发送接收消息双方约定的生成多项式 G ( x ) G(x) G(x)&#xff08;意思就是 G &#xff08; x ) G&#xff08;x) G&#xff08;x) 是已知的&#xff09;&#xff0…...

Mac软件卸载指南,简单易懂!

刚和Adobe分手&#xff0c;它却总在Library里给你写"回忆录"&#xff1f;卸载的Final Cut Pro像电子幽灵般阴魂不散&#xff1f;总是会有残留文件&#xff0c;别慌&#xff01;这份Mac软件卸载指南&#xff0c;将用最硬核的方式教你"数字分手术"&#xff0…...

【Zephyr 系列 10】实战项目:打造一个蓝牙传感器终端 + 网关系统(完整架构与全栈实现)

🧠关键词:Zephyr、BLE、终端、网关、广播、连接、传感器、数据采集、低功耗、系统集成 📌目标读者:希望基于 Zephyr 构建 BLE 系统架构、实现终端与网关协作、具备产品交付能力的开发者 📊篇幅字数:约 5200 字 ✨ 项目总览 在物联网实际项目中,**“终端 + 网关”**是…...

企业如何增强终端安全?

在数字化转型加速的今天&#xff0c;企业的业务运行越来越依赖于终端设备。从员工的笔记本电脑、智能手机&#xff0c;到工厂里的物联网设备、智能传感器&#xff0c;这些终端构成了企业与外部世界连接的 “神经末梢”。然而&#xff0c;随着远程办公的常态化和设备接入的爆炸式…...

Java求职者面试指南:Spring、Spring Boot、MyBatis框架与计算机基础问题解析

Java求职者面试指南&#xff1a;Spring、Spring Boot、MyBatis框架与计算机基础问题解析 一、第一轮提问&#xff08;基础概念问题&#xff09; 1. 请解释Spring框架的核心容器是什么&#xff1f;它在Spring中起到什么作用&#xff1f; Spring框架的核心容器是IoC容器&#…...

Java编程之桥接模式

定义 桥接模式&#xff08;Bridge Pattern&#xff09;属于结构型设计模式&#xff0c;它的核心意图是将抽象部分与实现部分分离&#xff0c;使它们可以独立地变化。这种模式通过组合关系来替代继承关系&#xff0c;从而降低了抽象和实现这两个可变维度之间的耦合度。 用例子…...

排序算法总结(C++)

目录 一、稳定性二、排序算法选择、冒泡、插入排序归并排序随机快速排序堆排序基数排序计数排序 三、总结 一、稳定性 排序算法的稳定性是指&#xff1a;同样大小的样本 **&#xff08;同样大小的数据&#xff09;**在排序之后不会改变原始的相对次序。 稳定性对基础类型对象…...

动态 Web 开发技术入门篇

一、HTTP 协议核心 1.1 HTTP 基础 协议全称 &#xff1a;HyperText Transfer Protocol&#xff08;超文本传输协议&#xff09; 默认端口 &#xff1a;HTTP 使用 80 端口&#xff0c;HTTPS 使用 443 端口。 请求方法 &#xff1a; GET &#xff1a;用于获取资源&#xff0c;…...

GO协程(Goroutine)问题总结

在使用Go语言来编写代码时&#xff0c;遇到的一些问题总结一下 [参考文档]&#xff1a;https://www.topgoer.com/%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/goroutine.html 1. main()函数默认的Goroutine 场景再现&#xff1a; 今天在看到这个教程的时候&#xff0c;在自己的电…...