当前位置:网站首页>Play with concurrency: what's the use of interruptedexception?
Play with concurrency: what's the use of interruptedexception?
2022-07-02 04:05:00 【Java knowledge hall】
InterruptedException How do exceptions come about ?
interrupt Used to interrupt the execution of threads
When the thread is in WAITING and TIMED_WAITING In the state of , If the interrupt Method will throw InterruptedException
When the thread is running , If the interrupt Method just marks a stop in the current thread , The logic of stop needs to be realized by ourselves
Thread Class provides the following 2 There are two methods to determine whether the thread is in interrupt state
- isInterrupted
- interrupted
this 2 All of them can judge the state , But there are subtle differences
@Test
public void testInterrupt() throws InterruptedException {
Thread thread = new Thread(() -> {
while (true) {
}
});
thread.start();
TimeUnit.MICROSECONDS.sleep(100);
thread.interrupt();
// true
System.out.println(thread.isInterrupted());
// true
System.out.println(thread.isInterrupted());
// true
System.out.println(thread.isInterrupted());
}
@Test
public void testInterrupt2() {
Thread.currentThread().interrupt();
// true
System.out.println(Thread.interrupted());
// false
System.out.println(Thread.interrupted());
// false
System.out.println(Thread.interrupted());
}
isInterrupted and interrupted The differences are as follows
Thread#isInterrupted: Test whether the thread is in interrupt state , Do not change the status flag after execution
Thread#interrupted: Test whether the thread is in interrupt state , After execution, change the interrupt flag to false
Reference blog
[1]
边栏推荐
- Blue Bridge Cup SCM digital tube skills
- C语言:逻辑运算和判断选择结构例题
- Handling of inconsistency between cursor and hinttext position in shutter textfield
- The first game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup
- Go variables and constants
- [personal notes] PHP common functions - custom functions
- Common sense of cloud server security settings
- MySQL advanced SQL statement 2
- 蓝湖的安装及使用
- Feature Engineering: summary of common feature transformation methods
猜你喜欢
Suggestions on settlement solution of u standard contract position explosion
Introduction to vmware workstation and vSphere
BGP experiment the next day
First acquaintance with string+ simple usage (II)
FAQ | FAQ for building applications for large screen devices
Typescript practice for SAP ui5
pip 安装第三方库
The 8th Blue Bridge Cup single chip microcomputer provincial competition
云服务器的安全设置常识
WPViewPDF Delphi 和 .NET 的 PDF 查看组件
随机推荐
Visual slam Lecture 3 -- Lie groups and Lie Algebras
Feature Engineering: summary of common feature transformation methods
Blue Bridge Cup single chip microcomputer sixth temperature recorder
软件测试人的第一个实战项目:web端(视频教程+文档+用例库)
手撕——排序
The 11th Blue Bridge Cup single chip microcomputer provincial competition
云服务器的安全设置常识
go 分支与循环
【力扣刷题】15.三数之和(双指针);17.电话号码的字母组合(递归回溯)
[JS -- map string]
【无线图传】基于FPGA的简易无线图像传输系统verilog开发,matlab辅助验证
Sorted out an ECS summer money saving secret, this time @ old users come and take it away
The 7th Blue Bridge Cup single chip microcomputer provincial competition
Wechat applet calculates the distance between the two places
Www2022 | know your way back: self training method of graph neural network under distribution and migration
The difference between vectorresize and reverse.
go 函数
Microsoft Research Institute's new book "Fundamentals of data science", 479 Pages pdf
Three ways for programmers to learn PHP easily and put chaos out of order
u本位合约爆仓清算解决方案建议