当前位置:网站首页>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
- Li Kou interview question 02.08 Loop detection
- First acquaintance with string+ simple usage (II)
- Analysis of the overall design principle of Nacos configuration center (persistence, clustering, information synchronization)
- Recyclerview add header
- Go variables and constants
- go 语言命名规范
- Pandora IOT development board learning (HAL Library) - Experiment 2 buzzer experiment (learning notes)
- Okcc why is cloud call center better than traditional call center?
- Wechat applet pull-down loading more waterfall flow loading
猜你喜欢

Finally got byte offer. The 25-year-old inexperienced perception of software testing is written to you who are still confused

Basic operations of MySQL database (based on tables)

66.qt quick QML Custom Calendar component (supports vertical and horizontal screens)

【leetcode】34. Find the first and last positions of elements in a sorted array

Sorted out an ECS summer money saving secret, this time @ old users come and take it away

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

【小技巧】使用matlab GUI以对话框模式读取文件

JVM knowledge points
![[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)
![[personnel density detection] matlab simulation of personnel density detection based on morphological processing and GRNN network](/img/11/4a8b52603e6e14a1ed6da1264dee57.png)
[personnel density detection] matlab simulation of personnel density detection based on morphological processing and GRNN network
随机推荐
Visual slam Lecture 3 -- Lie groups and Lie Algebras
Wechat applet calculates the distance between the two places
Wechat applet - realize the countdown of 60 seconds to obtain the mobile verification code (mobile number + verification code login function)
Influence of air resistance on the trajectory of table tennis
What is 5g industrial wireless gateway? What functions can 5g industrial wireless gateway achieve?
The first practical project of software tester: web side (video tutorial + document + use case library)
Use a mask to restrict the input of the qlineedit control
Today's plan: February 15, 2022
Yyds dry inventory compiler and compiler tools
Déchirure à la main - tri
Introduction to JSON usage scenarios and precautions
《西线无战事》我们才刚开始热爱生活,却不得不对一切开炮
Wechat applet pull-down loading more waterfall flow loading
The 9th Blue Bridge Cup single chip microcomputer provincial competition
Go function
Typescript practice for SAP ui5
手撕——排序
Nacos 配置中心整体设计原理分析(持久化,集群,信息同步)
Sword finger offer II 006 Sort the sum of two numbers in the array
go 语言命名规范