当前位置:网站首页>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]
边栏推荐
- [personnel density detection] matlab simulation of personnel density detection based on morphological processing and GRNN network
- okcc为什么云呼叫中心比传统呼叫中心更好?
- go 语言命名规范
- Set vscode. When double clicking, the selected string includes the $symbol - convenient for PHP operation
- Jetpack's livedata extension mediatorlivedata
- Where can I buy cancer insurance? Which product is better?
- PR zero foundation introductory guide note 2
- BiShe cinema ticket purchasing system based on SSM
- Wechat applet calculates the distance between the two places
- Delete the code you wrote? Sentenced to 10 months!
猜你喜欢

JVM知识点

蓝湖的安装及使用

Actual combat | use composite material 3 in application

BGP experiment the next day

【c语言】基础篇学习笔记

Which is better, industrial intelligent gateway or edge computing gateway? How to choose the right one?

Spring recruitment of Internet enterprises: Kwai meituan has expanded the most, and the annual salary of technical posts is up to nearly 400000
![[personal notes] PHP common functions - custom functions](/img/3d/d50622e3ddb08f654f30063e8226ac.jpg)
[personal notes] PHP common functions - custom functions

Influence of air resistance on the trajectory of table tennis

Go language introduction
随机推荐
Opencv learning example code 3.2.4 LUT
Learn more about materialapp and common attribute parsing in fluent
SQL:常用的 SQL 命令
How much is the tuition fee of SCM training class? How long is the study time?
【IBDFE】基于IBDFE的频域均衡matlab仿真
C language: examples of logical operation and judgment selection structure
Dare to go out for an interview without learning some distributed technology?
How should the team choose the feature branch development mode or trunk development mode?
The 11th Blue Bridge Cup single chip microcomputer provincial competition
Wechat applet - realize the countdown of 60 seconds to obtain the mobile verification code (mobile number + verification code login function)
Homework in Chapter 3 of slam course of dark blue vision -- derivative application of T6 common functions
LxC limits the number of CPUs
Yyds dry inventory compiler and compiler tools
The confusion I encountered when learning stm32
[personnel density detection] matlab simulation of personnel density detection based on morphological processing and GRNN network
SQL Yiwen get window function
Delete the code you wrote? Sentenced to 10 months!
The second game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup
Sorted out an ECS summer money saving secret, this time @ old users come and take it away
初识P4语言