中断机制-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

相关文章

SpringRetry重试机制之@Retryable注解与重试策略详解

《SpringRetry重试机制之@Retryable注解与重试策略详解》本文将详细介绍SpringRetry的重试机制,特别是@Retryable注解的使用及各种重试策略的配置,帮助开发者构建更加健... 目录引言一、SpringRetry基础知识二、启用SpringRetry三、@Retryable注解

Python中使用正则表达式精准匹配IP地址的案例

《Python中使用正则表达式精准匹配IP地址的案例》Python的正则表达式(re模块)是完成这个任务的利器,但你知道怎么写才能准确匹配各种合法的IP地址吗,今天我们就来详细探讨这个问题,感兴趣的朋... 目录为什么需要IP正则表达式?IP地址的基本结构基础正则表达式写法精确匹配0-255的数字验证IP地

MySQL高级查询之JOIN、子查询、窗口函数实际案例

《MySQL高级查询之JOIN、子查询、窗口函数实际案例》:本文主要介绍MySQL高级查询之JOIN、子查询、窗口函数实际案例的相关资料,JOIN用于多表关联查询,子查询用于数据筛选和过滤,窗口函... 目录前言1. JOIN(连接查询)1.1 内连接(INNER JOIN)1.2 左连接(LEFT JOI

SpringKafka错误处理(重试机制与死信队列)

《SpringKafka错误处理(重试机制与死信队列)》SpringKafka提供了全面的错误处理机制,通过灵活的重试策略和死信队列处理,下面就来介绍一下,具有一定的参考价值,感兴趣的可以了解一下... 目录引言一、Spring Kafka错误处理基础二、配置重试机制三、死信队列实现四、特定异常的处理策略五

Python 迭代器和生成器概念及场景分析

《Python迭代器和生成器概念及场景分析》yield是Python中实现惰性计算和协程的核心工具,结合send()、throw()、close()等方法,能够构建高效、灵活的数据流和控制流模型,这... 目录迭代器的介绍自定义迭代器省略的迭代器生产器的介绍yield的普通用法yield的高级用法yidle

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

kotlin中const 和val的区别及使用场景分析

《kotlin中const和val的区别及使用场景分析》在Kotlin中,const和val都是用来声明常量的,但它们的使用场景和功能有所不同,下面给大家介绍kotlin中const和val的区别,... 目录kotlin中const 和val的区别1. val:2. const:二 代码示例1 Java

Go标准库常见错误分析和解决办法

《Go标准库常见错误分析和解决办法》Go语言的标准库为开发者提供了丰富且高效的工具,涵盖了从网络编程到文件操作等各个方面,然而,标准库虽好,使用不当却可能适得其反,正所谓工欲善其事,必先利其器,本文将... 目录1. 使用了错误的time.Duration2. time.After导致的内存泄漏3. jsO

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La