当前位置:网站首页>线程终止的 4 种方式
线程终止的 4 种方式
2022-06-28 13:48:00 【星辰与晨曦】
正常运行结束
这个也是最常见的,指线程体执行完成,线程自动结束。
使用退出标志退出
在一般情况下,在 run 方法执行完毕的时候,线程会正常结束。然而,有些线程是后台线程,需要长时间运行,只有在系统满足某些特殊条件后,才能退出这些线程。这时可以使用一个变量来控制循环,比如设置一个 Boolean 类型的标志,并通过设置这个标志为 true 或 false 来控制 while 循环是否退出。
public class ThreadDemo extends Thread {
public volatile boolean exit = false;
@Override
public void run() {
while (!exit) {
//业务逻辑代码
}
}
}
在上面的代码中定义了一个退出的表示 exit,exit 的默认值为 false。在定义 exit 时使用了一个 Java 的关键字 volatile,这个关键字是用于保证 exit 线程同步安全的,也就是说在同一时刻只能有一个线程修改 exit 的值,在 exit 为true 的时候,while 循环退出,线程终止。
使用 Interrupt 方法终止线程
在使用 Interrupt 方法在终止线程的时候,是由两种情况的
- 线程处于阻塞状态
例如:在使用 sleep、调用锁的 wait 或者调用 socket 的 receive、accept 等方法的时候,会使线程处于阻塞状态。在调用线程的 interrupt 方法时,会抛出 InterruptException 异常,我们通过在代码中捕获异常,然后通过 break 跳出状态监测循环,结束这个线程的执行。通常很多人认为只要调用 interrupt 方法就会结束线程,这个理解其实是有问题的,一定是要先捕获 InterruptedException 异常后再通过 break 跳出循环,才能正常的结束 run 方法。
public class InterruptThread {
public static void main(String[] args) {
InterruptDemo thread = new InterruptDemo();
thread.start();
thread.interrupt();
}
}
class InterruptDemo extends Thread {
@Override
public void run() {
while (Thread.currentThread().isInterrupted()) {
try {
System.out.println("执行任务代码,出现了sleep");
Thread.sleep(1000);//写的异常,出现要抛出异常的错误
} catch (InterruptedException e) {
e.printStackTrace();
break;//在捕获到异常的时候,就可以退出循环了
}
}
}
}
- 线程处于未阻塞状态
在这个时候,使用 isInterrupted 方法判断线程的中断标志来退出循环。在调用 interrupt 方法的时候,中断标志会设置为 true,此时并不能立刻退出线程而是需要执行线程终止前的资源释放操作,等待资源释放完毕后方可安全退出该线程。
使用 stop 方法终止线程
使用 stop 方法终止线程是不安全的
在程序中可以直接调用 Thread.stop 方法来强制终止线程,但这是很危险的,就像突然把你的电脑电源断了,而不是正常结束,这样可能就会导致一些无法预测的后果。
在程序使用 Thread.sleep 方法终止线程的时候,该线程的子线程会抛出 ThreadDeatherror 错误,并且释放子线程锁持有的所有锁,加锁的代码块一般被用于保护数据的一致性,如果在调用 Thread.stop 方法后导致该线程锁持有的所有锁突然释放而使锁的资源不可控制。被保护的数据就可能出现不一致的情况,其他线程在使用这些被破坏的数据时,就有可能是程序运行错误,因此,并不推荐采用这种方式终止线程的。
边栏推荐
- Stackoverflow 2022 database annual survey
- Introduction to PWN (1) binary Basics
- 恒生电子:金融分布式数据库LightDB通过中国信通院多项测评
- iNFTnews | 科技巨头加快进军Web3和元宇宙
- 药物发现新方法,阿斯利康团队通过课程学习改进从头分子设计
- Analysis and processing of GPS data format [easy to understand]
- Pytorch Foundation
- Talk about exception again -- what happens when an exception is thrown?
- Luogu_ P1303 A*B Problem_ High precision calculation
- 真香啊!最全的 Pycharm 常用快捷键大全!
猜你喜欢
随机推荐
(原创)【MAUI】一步一步实现“悬浮操作按钮”(FAB,Floating Action Button)
Connected to rainwater series problems
How to open an account of Huatai Securities? How to handle the account opening is the safest
Yii2 connects to websocket service to realize that the server actively pushes messages to the client
行动诠释价值,城联优品韩董事长出席广东英德抗洪捐赠公益活动会
如何备份mysql_史上最全的MYSQL备份方法
真香啊!最全的 Pycharm 常用快捷键大全!
NPOI导出Excel并下载到客户端
China Radio and television 5g package is coming, lower than the three major operators, but not as low as expected
Introduction to PWN (1) binary Basics
What is generics and how to use generics analysis
求解汉诺塔问题
Action interprets value. The chairman of chenglian Youpin Han attended the Guangdong Yingde flood fighting donation public welfare event
PHP gets the number of digits and replaces it with the specified mantissa
Simple understanding of ThreadLocal
中国广电5G套餐来了,比三大运营商低,却没预期那么低
How to open an account on the flush? Is it safe
i++ , ++i
Regular matching numbers, English and English symbols
From PDB source code to frame frame object