当前位置:网站首页>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]
边栏推荐
- Three ways for programmers to learn PHP easily and put chaos out of order
- The difference between vectorresize and reverse.
- Blue Bridge Cup single chip microcomputer sixth temperature recorder
- Delete the code you wrote? Sentenced to 10 months!
- 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
- go 函数
- MySQL advanced SQL statement 2
- Go语言介绍
- MySQL error: expression 1 of select list is not in group by claim and contains nonaggre
- LxC limits the number of CPUs
猜你喜欢

Go语言介绍

JVM knowledge points

手撕——排序

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

C语言:逻辑运算和判断选择结构例题
![[untitled]](/img/53/cb61622cfcc73a347d2d5e852a5421.jpg)
[untitled]

Li Kou interview question 02.08 Loop detection
![[wireless image transmission] FPGA based simple wireless image transmission system Verilog development, matlab assisted verification](/img/77/4df7a1439ff1a53f94d409a19a47d6.png)
[wireless image transmission] FPGA based simple wireless image transmission system Verilog development, matlab assisted verification

First acquaintance with string+ simple usage (II)
随机推荐
集成底座方案演示说明
树莓派GPIO引脚控制红绿灯与轰鸣器
[personnel density detection] matlab simulation of personnel density detection based on morphological processing and GRNN network
[JS -- map string]
Cloud service selection of enterprises: comparative analysis of SaaS, PAAS and IAAs
How to solve the code error when storing array data into the database
Demonstration description of integrated base scheme
Fluent icon demo
First acquaintance with P4 language
Okcc why is cloud call center better than traditional call center?
WPViewPDF Delphi 和 .NET 的 PDF 查看组件
Sword finger offer II 006 Sort the sum of two numbers in the array
SQL: common SQL commands
Installation et utilisation du lac bleu
Go function
The difference between vectorresize and reverse.
PIP installation of third-party libraries
First acquaintance with string+ simple usage (II)
Where can I buy cancer insurance? Which product is better?
Finally got byte offer. The 25-year-old inexperienced perception of software testing is written to you who are still confused