当前位置:网站首页>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]
边栏推荐
- The 10th Blue Bridge Cup single chip microcomputer provincial competition
- Visual slam Lecture 3 -- Lie groups and Lie Algebras
- 10 minutes to understand CMS garbage collector in JVM
- Lei Jun wrote a blog when he was a programmer. It's awesome
- Microsoft Research Institute's new book "Fundamentals of data science", 479 Pages pdf
- [personal notes] PHP common functions - custom functions
- Today's plan: February 15, 2022
- Raspberry pie GPIO pin controls traffic light and buzzer
- Basic operations of MySQL database (based on tables)
- JVM knowledge points
猜你喜欢

go 包的使用

BiShe cinema ticket purchasing system based on SSM

How much is the tuition fee of SCM training class? How long is the study time?

手撕——排序

Www 2022 | rethinking the knowledge map completion of graph convolution network

Delete the code you wrote? Sentenced to 10 months!
![[Li Kou brush questions] 15 Sum of three numbers (double pointer); 17. Letter combination of phone number (recursive backtracking)](/img/5e/81e613370c808c63665c14298f9a39.png)
[Li Kou brush questions] 15 Sum of three numbers (double pointer); 17. Letter combination of phone number (recursive backtracking)

JVM知识点

The 6th Blue Bridge Cup single chip microcomputer provincial competition

Nacos 配置中心整体设计原理分析(持久化,集群,信息同步)
随机推荐
go 分支与循环
Influence of air resistance on the trajectory of table tennis
Thinkphp6 limit interface access frequency
Which product of anti-cancer insurance is better?
初识P4语言
go 函数
手撕——排序
蓝湖的安装及使用
Nacos 配置中心整体设计原理分析(持久化,集群,信息同步)
[live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!
How to model noise data? Hong Kong Baptist University's latest review paper on "label noise representation learning" comprehensively expounds the data, objective function and optimization strategy of
Target free or target specific: a simple and effective zero sample position detection comparative learning method
毕设-基于SSM电影院购票系统
LCM of Spreadtrum platform rotates 180 °
Delete the code you wrote? Sentenced to 10 months!
【leetcode】81. Search rotation sort array II
5G时代全面到来,浅谈移动通信的前世今生
Hand tear - sort
Feature Engineering: summary of common feature transformation methods
Go language naming specification