当前位置:网站首页>Thread interrupt
Thread interrupt
2022-06-09 21:23:00 【Grapefruit DZH】
Thread the interrupt
Thread interruption means that a thread is interrupted by other threads during its operation , It is associated with stop The big difference :stop The system forces the thread to terminate , Thread interrupt sends an interrupt signal to the target thread If the target thread does not receive the thread interrupt signal and ends the thread , The thread will not terminate , Whether to exit or execute other logic is determined by the target thread
example 1 Interrupt failed
package com.starry.codeview.threads.P05_ThreadInterrupt;
/**
* Thread interrupt failed , Because the target thread received the interrupt signal and did not handle it
*/
public class T01_ThreadInterrupt_Failed {
static int i = 10;
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
while (i++ < 56) {
System.out.println("t1 Threads execute ");
Thread.yield();
}
});
t1.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t1.interrupt();
}
}example 2 Interrupt success
package com.starry.codeview.threads.P05_ThreadInterrupt;
/**
* Thread interrupt succeeded , The target thread receives the interrupt signal and processes it
*/
public class T02_ThreadInterrupt_Successed {
static int i = 10;
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
while (i++ < 56) {
System.out.println("t1 Threads execute ");
Thread.yield();
if (Thread.currentThread().isInterrupted()) {
System.out.println("t1 Thread interrupted , sign out ");
return;
}
}
});
t1.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t1.interrupt();
}
}example 3 Sleep Interrupt failed
package com.starry.codeview.threads.P05_ThreadInterrupt;
/**
* Thread interrupt failed ,Sleep Thread interrupt encountered catch To the exception, the interrupt flag will be cleared , So the thread will continue to loop
*/
public class T03_ThreadInterrupt_Sleep_Failed {
static int i = 10;
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
while (i++ < 56) {
System.out.println("t1 Threads execute ");
Thread.yield();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
System.out.println("t1 Threads sleep Interrupted ");
e.printStackTrace();
}
}
});
t1.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t1.interrupt();
}
}example 4 Sleep Interrupt success
package com.starry.codeview.threads.P05_ThreadInterrupt;
/**
* Thread interrupt failed ,Sleep Thread interrupt encountered catch To the exception, the interrupt flag will be cleared , however catch An interrupt handling action is made in the exception block , So the interruption succeeded !!!
*/
public class T04_ThreadInterrupt_Sleep_Successed {
static int i = 10;
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
while (i++ < 56) {
System.out.println("t1 Threads execute ");
Thread.yield();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("t1 Threads sleep Interrupted ");
Thread.currentThread().interrupt();
}
}
});
t1.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t1.interrupt();
}
}Copyright belongs to :dingzhenhua Link to this article :https://www.dcmickey.cn/Java/221.html When reprinted, please indicate the source and this statement
边栏推荐
猜你喜欢

排序-快速排序

Sort - quick sort

To simplify, one annotation completes the database configuration

MySQL exception: the server time zone value 'xxx' solution

深入理解 Go Modules 的 go.mod 與 go.sum

Cvpr2022 oral | cross view transformer for semantic segmentation of real-time map views

瀏覽器無法打開百度,別的可以正常打開

Lambda Exception

Mqtt graphical client-mqttx installation and use tutorial

es自动停止
随机推荐
Kubevirt network source code analysis (2)
maximum likelihood estimation
How Bi makes SaaS products have a "sense of security" and "sensitivity" (Part I)
GBase8s数据库select子句3
Tke builds efk log service
Modbus协议与SerialPort端口读写
Le navigateur ne peut pas ouvrir Baidu, d'autres peuvent être ouverts normalement
搭建ngrok服务器,实现内网穿透服务,实现外网到内网的在线访问
Kubevirt network source code analysis
A consistent friend
Kubernetes native cicd:tekton Hello World
MFC connection database shows no data source name found and no default driver specified
华为云工业智能中枢,为加速工业智能化升级提供新动能
MediaTek: the market demand will not disappear, and the compound annual growth rate of revenue in the next three years will exceed 14%
二叉树的层序遍历
es自动停止
Mysql:1062 Duplicate entry '1' for key 'PRIMARY'
Numpy duplicate data
Set up ngrok server, realize intranet penetration service, and realize online access from external network to internal network
The server responded that static resources were not loaded