当前位置:网站首页>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() 是静态方法,属于类级别的方法。
边栏推荐
- 第二批入围企业公示!年度TOP100智能网联供应商评选
- 华大04a工作模式/低功耗模式
- 进程的通信方式
- EasyBypass
- “阿里健康”们的逻辑早就变了
- Zero code can apply data visualization to enterprise management
- 60 个神级 VS Code 插件!!
- Shutter precautions for using typedef
- Reduce the pip to the specified version (upgrade the PIP through CMP and reduce it to the original version)
- Flutter 库冲突问题解决
猜你喜欢
![[notes of Wu Enda] convolutional neural network](/img/19/2cac17010c29cbd5ba245de105d6c1.png)
[notes of Wu Enda] convolutional neural network

华大04a工作模式/低功耗模式

The process from troubleshooting to problem solving: the browser suddenly failed to access the web page, error code: 0x80004005, and the final positioning: "when the computer turns on the hotspot, the

Yida technology signed a contract with seven wolves to help the digital transformation of "Chinese men's wear leader"

60 divine vs Code plug-ins!!

性能测试工具wrk安装使用详解

是真干不过00后,给我卷的崩溃,想离职了...
![Find the maximum value in each tree row [extension of one of the hierarchical traversals]](/img/5b/81ff20b61c0719ceb6873e44878859.png)
Find the maximum value in each tree row [extension of one of the hierarchical traversals]

Detailed installation and use of performance test tool wrk

leetcode:45. 跳跃游戏 II【经典贪心】
随机推荐
短视频商城系统,scroll-view如何自适应页面剩余高度
Multithreaded finalization
Flutter: Unsupported value: false/true
为什么有的程序员能力一般却能拿到好offer?
Balanced binary search tree
Publicity of the second batch of shortlisted enterprises! Annual Top100 smart network supplier selection
cv2导包时报Could not find a version that satisfies the requirement cv2 (from versions: none)
权限想要细化到按钮,怎么做?
first-order-model实现照片动起来(附工具代码) | 机器学习
Ansible basic configuration
产业互联网时代,并不存在传统意义上的互联网
leetcode:515. Find the maximum value in each tree row [brainless BFS]
【OpenCV 例程200篇】209. HSV 颜色空间的彩色图像分割
Main steps of system test
985测试工程师被吊打,学历和经验到底谁更重要?
Machine learning: gradient descent method
直播软件app开发,左右自动滑动的轮播图广告
SAP interface debug setting external breakpoints
Resolving the conflict problem of the flutter Library
You are using pip version 21.1.2; however, version 22.1.2 is available