当前位置:网站首页>interrupt、interrupted 、isInterrupted 区别
interrupt、interrupted 、isInterrupted 区别
2022-06-24 19:34:00 【Nice2cu_Code】
interrupt、interrupted 、isInterrupted 区别
一、interrupt
一个线程不应该由其他线程来强制中断或停止,而是应该由线程自己自行停止。所以,Thread.stop, Thread.suspend, Thread.resume 都已经被废弃了。
而 Thread.interrupt() 的作用其实也不是中断线程,而是通知线程应该中断了,具体到底中断还是继续运行,应该由被通知的线程自己处理。
具体来说,当一个线程调用 interrupt() 方法时:
① 如果线程处于阻塞状态(例如处于 sleep, wait, join 等状态),那么线程将立即退出阻塞状态,并抛出一个 InterruptedException 异常,仅此而已,中断标记仍为 false。
② 如果线程处于正常活动状态,那么会将该线程的中断标志设置为 true,仅此而已,不会抛出异常。被设置中断标志的线程将继续正常运行,不受影响。
③ 如果一个线程处于 park 状态(休眠)时(调用 Unsafe.park() 或者 LockSupport.park()),调用 interrupt() 方法会将中断标记设置为true并且立马返回,但是不会抛出InterruptedException 异常。
综上所述,interrupt() 方法并不能真正的中断线程,需要被调用的线程自己进行处理才行。
也就是说,如果一个线程如果有被中断的需求,那么就可以这样做:
① 在正常运行任务时,经常检查本线程的中断标志位,如果中断标志被设置为 true 就自行停止线程。
② 处于阻塞状态的线程应该正确处理 InterruptedException 异常,例如 catch 异常后就停止线程。
二、interrupted
源码如下:
public static boolean interrupted() {
//调用了当前线程的isInterrupted方法,参数传递true
return currentThread().isInterrupted(true);
}
三、isInterrupted
调用 isInterrupted() 方法对应的源码如下:
public boolean isInterrupted() {
//继调用isInterrupted方法,只不过参数传递false
return isInterrupted(false);
}
interrupted 和 isInterrupted 这两个方法有两个主要区别:
- interrupted 是作用于当前线程,isInterrupted 是作用于调用该方法的线程对象所对应的线程。(线程对象对应的线程不一定是当前运行的线程。例如可以在A线程中调用B线程对象的 isInterrupted 方法)
- 这两个方法最终都会调用同一个方法,只不过参数一个是true,一个是false。
最终调用的 isInterrupted() 方法源码如下:
//是一个本地方法
private native boolean isInterrupted(boolean ClearInterrupted);
通过参数名可以得知参数的作用是是否要清除状态位。
如果这个参数为true,表示要清除线程的状态标志,也就是置为false;如果参数为false,直接返回线程的状态位,不会对状态进行修改。
isInterrupted() 和 interrupt() 是实例方法,必须要通过对象实例来调用;interrupted() 是静态方法,属于类级别的方法。
边栏推荐
- Redis+caffeine two-level cache enables smooth access speed
- 字符串习题总结2
- Heartless sword Chinese English bilingual poem 003 The sea of books
- 产业互联网时代,并不存在传统意义上的互联网
- The profound meaning of unlimited ecological development in Poka -- Multidimensional Interpretation of parallel chain
- [untitled]
- In the first year of L2, arbitrum nitro was upgraded to bring more compatible and efficient development experience
- Several schemes of traffic exposure in kubernetes cluster
- How to refine permissions to buttons?
- 揭秘B站,程序员穿女装敲代码,效率更高是真的吗?
猜你喜欢

Réduire le PIP à la version spécifiée (mettre à jour le PIP avec pycharm avant de le réduire à la version originale)

是真干不过00后,给我卷的崩溃,想离职了...

leetcode_ 191_ 2021-10-15

I really want to send a bunch of flowers
![[notes of Wu Enda] convolutional neural network](/img/19/2cac17010c29cbd5ba245de105d6c1.png)
[notes of Wu Enda] convolutional neural network
![leetcode:515. Find the maximum value in each tree row [brainless BFS]](/img/87/1926d783fb6f8d8439213d86b5da40.png)
leetcode:515. Find the maximum value in each tree row [brainless BFS]

A pit in try with resources

Notes on writing questions (18) -- binary tree: common ancestor problem

虚拟人的产业发展现状

try-with-resources 中的一个坑,注意避让
随机推荐
Redis+caffeine two-level cache enables smooth access speed
Graduation design of phase 6 of the construction practice camp
为什么有的程序员能力一般却能拿到好offer?
短视频商城系统,scroll-view如何自适应页面剩余高度
leetcode:55. Jumping game [classic greed]
Balanced binary search tree
ansible基本配置
印刷行业的ERP软件的领头羊
“阿里健康”们的逻辑早就变了
第二批入围企业公示!年度TOP100智能网联供应商评选
降低pip到指定版本(通过cmp升级pip,在降低到原来版本)
排查到解决问题的过程:浏览器突然无法访问网页,错误代码:0x80004005,最终定位:“电脑打开热点,电脑就不能上网了”
St Table + two points
TKKC round#3
You are using pip version 21.1.2; however, version 22.1.2 is available
first-order-model实现照片动起来(附工具代码) | 机器学习
Stl+ tree
leetcode-201_ 2021_ 10_ seventeen
Resolving the conflict problem of the flutter Library
想当测试Leader,这6项技能你会吗?