当前位置:网站首页>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]
边栏推荐
- 【小技巧】使用matlab GUI以对话框模式读取文件
- QT designer plug-in implementation of QT plug-in
- [Li Kou brush questions] 15 Sum of three numbers (double pointer); 17. Letter combination of phone number (recursive backtracking)
- okcc为什么云呼叫中心比传统呼叫中心更好?
- How to solve the code error when storing array data into the database
- Monkey测试
- Pandora IOT development board learning (HAL Library) - Experiment 2 buzzer experiment (learning notes)
- Blue Bridge Cup SCM digital tube skills
- 【leetcode】34. Find the first and last positions of elements in a sorted array
- 集成底座方案演示说明
猜你喜欢

Pandora IOT development board learning (RT thread) - Experiment 1 LED flashing experiment (learning notes)
![[JS event -- event flow]](/img/fe/199890b082845f68b65f25056e6f29.jpg)
[JS event -- event flow]

《动手学深度学习》(二)-- 多层感知机

Learn more about materialapp and common attribute parsing in fluent

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

Dare to go out for an interview without learning some distributed technology?

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

Suggestions on settlement solution of u standard contract position explosion

PR zero foundation introductory guide note 2

Realizing deep learning framework from zero -- Introduction to neural network
随机推荐
The 11th Blue Bridge Cup single chip microcomputer provincial competition
【leetcode】34. Find the first and last positions of elements in a sorted array
The first game of the 12th Blue Bridge Cup single chip microcomputer provincial competition
L'avènement de l'ère 5G, une brève discussion sur la vie passée et présente des communications mobiles
手撕——排序
Welcome the winter vacation multi school league game 2 partial solution (B, C, D, F, G, H)
《西线无战事》我们才刚开始热爱生活,却不得不对一切开炮
Microsoft Research Institute's new book "Fundamentals of data science", 479 Pages pdf
WPViewPDF Delphi 和 .NET 的 PDF 查看组件
How to solve the problem that objects cannot be deleted in Editor Mode
5g era is coming in an all-round way, talking about the past and present life of mobile communication
Demonstration description of integrated base scheme
[personal notes] PHP common functions - custom functions
Influence of air resistance on the trajectory of table tennis
Installation et utilisation du lac bleu
pip 安装第三方库
Okcc why is cloud call center better than traditional call center?
Yyds dry inventory compiler and compiler tools
Which is better, industrial intelligent gateway or edge computing gateway? How to choose the right one?
【力扣刷题】15.三数之和(双指针);17.电话号码的字母组合(递归回溯)