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

相关文章

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

Hadoop企业开发案例调优场景

需求 (1)需求:从1G数据中,统计每个单词出现次数。服务器3台,每台配置4G内存,4核CPU,4线程。 (2)需求分析: 1G / 128m = 8个MapTask;1个ReduceTask;1个mrAppMaster 平均每个节点运行10个 / 3台 ≈ 3个任务(4    3    3) HDFS参数调优 (1)修改:hadoop-env.sh export HDFS_NAMENOD

第10章 中断和动态时钟显示

第10章 中断和动态时钟显示 从本章开始,按照书籍的划分,第10章开始就进入保护模式(Protected Mode)部分了,感觉从这里开始难度突然就增加了。 书中介绍了为什么有中断(Interrupt)的设计,中断的几种方式:外部硬件中断、内部中断和软中断。通过中断做了一个会走的时钟和屏幕上输入字符的程序。 我自己理解中断的一些作用: 为了更好的利用处理器的性能。协同快速和慢速设备一起工作

性能分析之MySQL索引实战案例

文章目录 一、前言二、准备三、MySQL索引优化四、MySQL 索引知识回顾五、总结 一、前言 在上一讲性能工具之 JProfiler 简单登录案例分析实战中已经发现SQL没有建立索引问题,本文将一起从代码层去分析为什么没有建立索引? 开源ERP项目地址:https://gitee.com/jishenghua/JSH_ERP 二、准备 打开IDEA找到登录请求资源路径位置

深入探索协同过滤:从原理到推荐模块案例

文章目录 前言一、协同过滤1. 基于用户的协同过滤(UserCF)2. 基于物品的协同过滤(ItemCF)3. 相似度计算方法 二、相似度计算方法1. 欧氏距离2. 皮尔逊相关系数3. 杰卡德相似系数4. 余弦相似度 三、推荐模块案例1.基于文章的协同过滤推荐功能2.基于用户的协同过滤推荐功能 前言     在信息过载的时代,推荐系统成为连接用户与内容的桥梁。本文聚焦于

JAVA智听未来一站式有声阅读平台听书系统小程序源码

智听未来,一站式有声阅读平台听书系统 🌟&nbsp;开篇:遇见未来,从“智听”开始 在这个快节奏的时代,你是否渴望在忙碌的间隙,找到一片属于自己的宁静角落?是否梦想着能随时随地,沉浸在知识的海洋,或是故事的奇幻世界里?今天,就让我带你一起探索“智听未来”——这一站式有声阅读平台听书系统,它正悄悄改变着我们的阅读方式,让未来触手可及! 📚&nbsp;第一站:海量资源,应有尽有 走进“智听

【区块链 + 人才服务】可信教育区块链治理系统 | FISCO BCOS应用案例

伴随着区块链技术的不断完善,其在教育信息化中的应用也在持续发展。利用区块链数据共识、不可篡改的特性, 将与教育相关的数据要素在区块链上进行存证确权,在确保数据可信的前提下,促进教育的公平、透明、开放,为教育教学质量提升赋能,实现教育数据的安全共享、高等教育体系的智慧治理。 可信教育区块链治理系统的顶层治理架构由教育部、高校、企业、学生等多方角色共同参与建设、维护,支撑教育资源共享、教学质量评估、

客户案例:安全海外中继助力知名家电企业化解海外通邮困境

1、客户背景 广东格兰仕集团有限公司(以下简称“格兰仕”),成立于1978年,是中国家电行业的领军企业之一。作为全球最大的微波炉生产基地,格兰仕拥有多项国际领先的家电制造技术,连续多年位列中国家电出口前列。格兰仕不仅注重业务的全球拓展,更重视业务流程的高效与顺畅,以确保在国际舞台上的竞争力。 2、需求痛点 随着格兰仕全球化战略的深入实施,其海外业务快速增长,电子邮件成为了关键的沟通工具。

Java ArrayList扩容机制 (源码解读)

结论:初始长度为10,若所需长度小于1.5倍原长度,则按照1.5倍扩容。若不够用则按照所需长度扩容。 一. 明确类内部重要变量含义         1:数组默认长度         2:这是一个共享的空数组实例,用于明确创建长度为0时的ArrayList ,比如通过 new ArrayList<>(0),ArrayList 内部的数组 elementData 会指向这个 EMPTY_EL

如何在Visual Studio中调试.NET源码

今天偶然在看别人代码时,发现在他的代码里使用了Any判断List<T>是否为空。 我一般的做法是先判断是否为null,再判断Count。 看了一下Count的源码如下: 1 [__DynamicallyInvokable]2 public int Count3 {4 [__DynamicallyInvokable]5 get