当前位置:网站首页>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() 是静态方法,属于类级别的方法。
边栏推荐
- cv2导包时报Could not find a version that satisfies the requirement cv2 (from versions: none)
- leetcode:45. Jumping game II [classic greed]
- 【无标题】
- Cannot find reference 'imread' in 'appears in pycharm__ init__. py‘
- leetcode_ one thousand three hundred and sixty-five
- [untitled]
- In the first year of L2, arbitrum nitro was upgraded to bring more compatible and efficient development experience
- [untitled]
- KT6368A蓝牙芯片的主从机之前透传功能说明,2.4G跳频自动连接
- Rotate the square array of two-dimensional array clockwise by 90 °
猜你喜欢
![[notes of Wu Enda] convolutional neural network](/img/19/2cac17010c29cbd5ba245de105d6c1.png)
[notes of Wu Enda] convolutional neural network

降低pip到指定版本(通过PyCharm升级pip,在降低到原来版本)
![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]

Multithreaded finalization

理想L9,智能座舱新潮流

Junior college background, 2 years in Suning, 5 years in Ali. How can I get promoted quickly?

KT6368A蓝牙双模透传芯片软件版本选型说明

How to extract dates from web pages?

虚拟人的产业发展现状

Maximum flow problem
随机推荐
Resolving the conflict problem of the flutter Library
ansible基本配置
PyCharm 中出现Cannot find reference ‘imread‘ in ‘__init__.py‘
04A中断的配置
EasyBypass
短视频商城系统,scroll-view如何自适应页面剩余高度
如何抓手机的包进行分析,Fiddler神器或许能帮到您!
Machine learning: linear regression
The profound meaning of unlimited ecological development in Poka -- Multidimensional Interpretation of parallel chain
first-order-model实现照片动起来(附工具代码) | 机器学习
Yida technology signed a contract with seven wolves to help the digital transformation of "Chinese men's wear leader"
Information update on automatic control principle
The logic of "Ali health" has long changed
Guava中这些Map的骚操作,让我的代码量减少了50%
产业互联网时代,并不存在传统意义上的互联网
leetcode1863_ 2021-10-14
Cannot find reference 'imread' in 'appears in pycharm__ init__. py‘
华大04a工作模式/低功耗模式
cv2导包时报Could not find a version that satisfies the requirement cv2 (from versions: none)
Flutter 库冲突问题解决