当前位置:网站首页>终止线程使用interrupt
终止线程使用interrupt
2022-04-21 13:51:00 【linjun20】
public class ThreadInterrupted
{
public static void main(String[] args) throws InterruptedException {
ThreadDemo demo1 = new ThreadDemo("线程1");
ThreadDemo demo2 = new ThreadDemo("线程2");
demo1.start();
demo2.start();
// demo1.stop();
Thread.sleep(1000);
demo1.interrupt();
}
}
class ThreadDemo extends Thread{
public ThreadDemo(String name) {
this.setName(name);
}
@Override
public void run() {
while(!interrupted()) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" is running");
}
}
}
1.stop()方法:用stop() 方法会真的杀死线程,不给线程喘息的机会,如果线程持有 ReentrantLock 锁,被 stop() 的线程并不会自动调用 ReentrantLock 的 unlock() 去释放锁,那其他线程就再也没机会获得 ReentrantLock 锁,这实在是太危险了。所以该方法就不建议使用了,类似的方法还有 suspend() 和 resume() 方法,这两个方法同样也都不建议使用了
2.interrupt():从线程的生命周期了解到,一个线程要终止,必须要从runnable状态到terminated。而线程可能处于休眠状态,interrupt方法恰好提供了此功能,它可以将休眠状态的线程转换到runnable状态,并置标志位为true(此时Thread.currentThread().isInterrupted()==true)。
版权声明
本文为[linjun20]所创,转载请带上原文链接,感谢
https://blog.csdn.net/linjun20/article/details/121182350
边栏推荐
- <2021SC@SDUSC>山东大学软件工程应用与实践JPress代码分析(十二)
- The importance of computing edge in Networkx: edge intermediate number or intermediate centrality edge_ betweenness
- Chapter 1 basic knowledge of password - Summary of commercial password application and security evaluation
- 应急响应笔记
- Zabbix5 series - creating auto discovery templates (XVI)
- CAS and atomic atomic operation classes for concurrent programming
- 双归上行--主备/负载
- SQL注入之sqli-labs等(安装,配置)
- EsgynDB CQD-traf_ lock_ ddl
- 山东大学项目实训树莓派提升计划二期(三)SSH远程连接
猜你喜欢

pytorch geometric中为何要将稀疏邻接矩阵写成转置的形式adj_t

深度学习与图像识别:原理与实践 笔记Day_18(目标检测)

《商用密码应用与安全性评估》第四章 密码应用安全性评估实施要点-小结

山东大学项目实训树莓派提升计划二期(七)对象和类

RHCE搭建简单web网站

mysql-三星索引和cost值成本计算

Zabbix5系列-创建自动发现模板 (十六)

< 2021SC@SDUSC > Application and practice of software engineering in Shandong University jpress code analysis (8)

园区网架构

山东大学项目实训树莓派提升计划二期(一)项目概述、树莓派简介
随机推荐
Trim function of esgyndb
< 2021SC@SDUSC > Application and practice of software engineering in Shandong University jpress code analysis (5)
Oracle backup failed with error ora-04063: View sys KU_ RADM_ FPTM_ VIEW has errors
CAS and atomic atomic operation classes for concurrent programming
How does Jupiter notebook copy multiple code blocks / cells across files (to another file)
mysql-连接查询成本和成本统计数据辨析
Web -- user registration interface
word2vec和node2vec笔记(更新ing)
Summary of several problems of esgyndb using JDBC UDR to access remote trafodion
深度学习与图像识别:原理与实践 笔记Day_18(目标检测)
<2021SC@SDUSC>山东大学软件工程应用与实践JPress代码分析(九)
Digital signature, public-private key encryption and decryption of RSA
认识与分析日志文件
< 2021SC@SDUSC > Application and practice of software engineering in Shandong University jpress code analysis (11)
Chapter III commercial password standards and product applications of commercial password application and security evaluation - Summary
The data show how fierce eth burns
山东大学项目实训树莓派提升计划二期(三)SSH远程连接
2021-10-21软件测试理论
iscsi
Chapter II commercial password application and security evaluation policies and regulations on commercial password application and security evaluation - Summary of deleted version