当前位置:网站首页>123. how to stop a thread?
123. how to stop a thread?
2022-07-01 09:27:00 【Hip hop geek】
Thread stop , How to stop a thread ?
stop Potential safety hazard , Outdated !
[1] adopt control run()
Method , To control the thread termination ;
class Demo3 extends Thread{
// By controlling run Method , To control the thread termination
private boolean isrun;
public boolean isIsrun(){
return isrun;
}
public void setIsrun(boolean isrun){
this.isrun = isrun;
}
@SneakyThrows
@Override
public void run(){
while (isrun){
Thread.sleep(400);
System.out.println(" Thread running !");
}
System.out.println(" Sub thread running :"+ System.currentTimeMillis());
}
}
class Demo4{
public static void main(String[] args) throws InterruptedException {
Demo3 demo3 = new Demo3();
// By controlling the dead loop method in the thread
demo3.setIsrun(true);
demo3.start();
Thread.sleep(5000);
demo3.setIsrun(false);
}
}
[2] Through threads interrupt()
【 principle 】 Controls how exceptions are thrown , Execution of control methods ;
class Demo4{
public static void main(String[] args) throws InterruptedException {
// Through threads interrupt Control the end of the thread
//1. Create a thread
Thread thread = new Thread(()->{
while (true){
try{
Thread.sleep(500);
//isInterruputed Thread interrupt ignored , Because it is not active at the time of interruption
// The thread of will return false The method of ;
System.out.println(Thread.interrupted());
boolean b = Thread.currentThread().isInterrupted();
System.out.println(" The child thread is running ..."+b);
}catch (Exception e){
System.out.println(" Thread the interrupt ");
return;
}
}
});
//2. Start thread
thread.start();
// Sleep 3 second
thread.sleep(10000);
// Execution thread interrupt
// In fact, it is in the form of throwing exceptions Execution of control methods
// That is, by calling interrupt() Then an exception will be thrown , Is to stop the thread
thread.interrupt();
}
}
[3] Brutal Use stop
:stop The method is out of date , But it can still stop ;
边栏推荐
- Analysis and solution of JS this loss
- Introduction to mt7628k eCos development
- tensorrt yolov5_ trt. Py comments
- ES6-const本质与完全不可改实现(Object.freeze)
- 利用闭包实现私有变量
- Differences between JS valueof and toString
- Log4j 日志框架
- ES6 decoupling top-level objects from windows
- SQL学习笔记(02)——数据库表操作
- 【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + MQ系列 + NodeJs本地服务 + MySql存储
猜你喜欢
随机推荐
手指点击屏幕就模拟进入F11进入全屏
利用闭包实现私有变量
[ESP nanny level tutorial] crazy completion chapter - Case: gy906 infrared temperature measurement access card swiping system based on the Internet of things
计网01-物理层
JS rewrite their own functions
Clickhouse: Test on query speed of A-share minute data [Part 2]
SQL学习笔记(03)——数据约束关系
delete和delete[]引发的问题
node. How to implement the SQL statement after JS connects to the database?
FAQ | FAQ for building applications for large screen devices
ES6 const essence and completely immutable implementation (object.free)
SDN_简单总结
闭包实现迭代器效果
Day06 branch structure and cycle (III)
Record a redis timeout
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的温湿度监控系统
Football and basketball game score live broadcast platform source code /app development and construction project
Promise asynchronous programming
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于物联网的GY906红外测温门禁刷卡系统
Click the screen with your finger to simulate F11 and enter the full screen