当前位置:网站首页>Interrupt, interrupted, isinterrupted differences
Interrupt, interrupted, isinterrupted differences
2022-06-24 22:21:00 【Nice2cu_ Code】
interrupt、interrupted 、isInterrupted difference
List of articles
One 、interrupt
A thread should not be forced to interrupt or stop by other threads , It should be stopped by the thread itself . therefore ,Thread.stop, Thread.suspend, Thread.resume It's been abandoned .
and Thread.interrupt()
In fact, it's not to interrupt threads , It is The notification thread should be broken , Whether to interrupt or continue to run , It should be handled by the notified thread itself .
say concretely , When a thread calls interrupt()
When the method is used :
① If the thread is blocked ( For example, in the sleep, wait, join Equal state ), Then the thread will immediately exit the blocking state , And throw a InterruptedException
abnormal , That's it , Interrupt flag remains false.
② If the thread is active , The thread's interrupt flag will be set to true, That's it , Does not throw an exception . The thread with interrupt flag will continue to run normally , Unaffected .
③ If a thread is in park state ( Sleep ) when ( call Unsafe.park()
perhaps LockSupport.park()
), call interrupt()
Method sets the interrupt flag to true And immediately return , however Don't throw InterruptedException
abnormal .
in summary ,interrupt()
Methods don't really interrupt threads , It needs to be handled by the called thread itself .
in other words , If a thread needs to be interrupted , So you can do this :
① In the normal operation of the task , Check the interrupt flag bit of this thread frequently , If the interrupt flag is set to true Just stop the thread .
② Threads that are blocked should handle InterruptedException abnormal , for example catch Stop the thread after the exception .
Two 、interrupted
Source code is as follows :
public static boolean interrupted() {
// Called the of the current thread isInterrupted Method , Parameter passing true
return currentThread().isInterrupted(true);
}
3、 ... and 、isInterrupted
call isInterrupted()
The source code of the method is as follows :
public boolean isInterrupted() {
// Subsequent call isInterrupted Method , It's just parameter passing false
return isInterrupted(false);
}
interrupted and isInterrupted There are two main differences between the two methods :
- interrupted Is for the current thread ,isInterrupted Is the thread corresponding to the thread object that calls the method .( The thread corresponding to the thread object is not necessarily the currently running thread . For example, it can be in A Call in thread B Of Thread objects isInterrupted Method )
- Both methods end up calling the same method , Only one parameter is true, One is false.
Finally called isInterrupted()
The source code of the method is as follows :
// It's a local approach
private native boolean isInterrupted(boolean ClearInterrupted);
From the parameter name, you can know that the function of the parameter is Whether to clear the status bit .
If this parameter is zero true, Indicates the status flag of the thread to be cleared , That is to say, set it as false; If the parameter is false, Directly return the status bit of the thread , The state is not modified .
isInterrupted() and interrupt() It's an instance method , You must call... Through an object instance ;interrupted() It's a static method , Methods at the class level .
边栏推荐
- Flutter: Unsupported value: false/true
- AQS源码分析
- 04A中断的配置
- Industrial development status of virtual human
- Introduction, installation and use of postman tool
- Description of transparent transmission function before master and slave of kt6368a Bluetooth chip, 2.4G frequency hopping automatic connection
- TCP RTT测量妙计
- CV2 package guide times could not find a version that satisfies the requirement CV2 (from versions: none)
- Implementation of heap sort and quick sort principle
- YGG 近期游戏合作伙伴一览
猜你喜欢
干货丨产品的可行性分析要从哪几个方面入手?
YGG 近期游戏合作伙伴一览
一个女孩子居然做了十年硬件。。。
leetcode:45. Jumping game II [classic greed]
985测试工程师被吊打,学历和经验到底谁更重要?
Xinlou: Huawei's seven-year building journey of sports health
Embedded development: tips and tricks -- clean jump from boot loader to application code
leetcode_ 191_ 2021-10-15
Huada 04A operating mode / low power consumption mode
如何抓手机的包进行分析,Fiddler神器或许能帮到您!
随机推荐
“阿里健康”们的逻辑早就变了
Embedded development: tips and tricks -- clean jump from boot loader to application code
leetcode:515. 在每个树行中找最大值【无脑bfs】
leetcode:45. Jumping game II [classic greed]
First order model realizes photo moving (with tool code) | machine learning
Mysql 通过表明获取字段以及注释
Servlet详解
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
EasyBypass
Notes on writing questions (18) -- binary tree: common ancestor problem
OA system -- save the verification code to session
You are using pip version 21.1.2; however, version 22.1.2 is available
AQS源码分析
PostMan工具介绍及安装使用
Disk structure
A pit in try with resources
壹沓科技签约七匹狼,助力「中国男装领导者」数字化转型
性能测试工具wrk安装使用详解
Docker 安装 Redis-5.0.12,详细步骤
KT6368A蓝牙双模透传芯片软件版本选型说明