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

2023-10-16 22:28

本文主要是介绍中断机制-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源码分析、中断协商案例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/221174

相关文章

Go使用pprof进行CPU,内存和阻塞情况分析

《Go使用pprof进行CPU,内存和阻塞情况分析》Go语言提供了强大的pprof工具,用于分析CPU、内存、Goroutine阻塞等性能问题,帮助开发者优化程序,提高运行效率,下面我们就来深入了解下... 目录1. pprof 介绍2. 快速上手:启用 pprof3. CPU Profiling:分析 C

Nginx之upstream被动式重试机制的实现

《Nginx之upstream被动式重试机制的实现》本文主要介绍了Nginx之upstream被动式重试机制的实现,可以通过proxy_next_upstream来自定义配置,具有一定的参考价值,感兴... 目录默认错误选择定义错误指令配置proxy_next_upstreamproxy_next_upst

MySQL表锁、页面锁和行锁的作用及其优缺点对比分析

《MySQL表锁、页面锁和行锁的作用及其优缺点对比分析》MySQL中的表锁、页面锁和行锁各有特点,适用于不同的场景,表锁锁定整个表,适用于批量操作和MyISAM存储引擎,页面锁锁定数据页,适用于旧版本... 目录1. 表锁(Table Lock)2. 页面锁(Page Lock)3. 行锁(Row Lock

Python爬虫selenium验证之中文识别点选+图片验证码案例(最新推荐)

《Python爬虫selenium验证之中文识别点选+图片验证码案例(最新推荐)》本文介绍了如何使用Python和Selenium结合ddddocr库实现图片验证码的识别和点击功能,感兴趣的朋友一起看... 目录1.获取图片2.目标识别3.背景坐标识别3.1 ddddocr3.2 打码平台4.坐标点击5.图

Springboot中分析SQL性能的两种方式详解

《Springboot中分析SQL性能的两种方式详解》文章介绍了SQL性能分析的两种方式:MyBatis-Plus性能分析插件和p6spy框架,MyBatis-Plus插件配置简单,适用于开发和测试环... 目录SQL性能分析的两种方式:功能介绍实现方式:实现步骤:SQL性能分析的两种方式:功能介绍记录

最长公共子序列问题的深度分析与Java实现方式

《最长公共子序列问题的深度分析与Java实现方式》本文详细介绍了最长公共子序列(LCS)问题,包括其概念、暴力解法、动态规划解法,并提供了Java代码实现,暴力解法虽然简单,但在大数据处理中效率较低,... 目录最长公共子序列问题概述问题理解与示例分析暴力解法思路与示例代码动态规划解法DP 表的构建与意义动

Spring排序机制之接口与注解的使用方法

《Spring排序机制之接口与注解的使用方法》本文介绍了Spring中多种排序机制,包括Ordered接口、PriorityOrdered接口、@Order注解和@Priority注解,提供了详细示例... 目录一、Spring 排序的需求场景二、Spring 中的排序机制1、Ordered 接口2、Pri

使用Navicat工具比对两个数据库所有表结构的差异案例详解

《使用Navicat工具比对两个数据库所有表结构的差异案例详解》:本文主要介绍如何使用Navicat工具对比两个数据库test_old和test_new,并生成相应的DDLSQL语句,以便将te... 目录概要案例一、如图两个数据库test_old和test_new进行比较:二、开始比较总结概要公司存在多

MySQL 缓存机制与架构解析(最新推荐)

《MySQL缓存机制与架构解析(最新推荐)》本文详细介绍了MySQL的缓存机制和整体架构,包括一级缓存(InnoDBBufferPool)和二级缓存(QueryCache),文章还探讨了SQL... 目录一、mysql缓存机制概述二、MySQL整体架构三、SQL查询执行全流程四、MySQL 8.0为何移除查

C#使用DeepSeek API实现自然语言处理,文本分类和情感分析

《C#使用DeepSeekAPI实现自然语言处理,文本分类和情感分析》在C#中使用DeepSeekAPI可以实现多种功能,例如自然语言处理、文本分类、情感分析等,本文主要为大家介绍了具体实现步骤,... 目录准备工作文本生成文本分类问答系统代码生成翻译功能文本摘要文本校对图像描述生成总结在C#中使用Deep