1、异常退出
在一个线程的执行单元中,是不允许抛出checked
异常的,不论Thread中的run方法,还是Runnable中的run方法,如果线程在运行过程中需要捕获checked异常并且判断是否还有运行下去的必要,那么此时可以将checked异常封装成unchecked
异常(RuntimeException
)抛出进而结束线程的生命周期。
2、正常关闭
2.1、线程结束生命周期正常结束
2.2、通过可中断方法
来决定是否退出
public class InterruptThreadExit2 {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread() {
@Override
public void run() {
System.out.println("I will start work → " + new Date());
for (; ; ) {
//working.
try {
TimeUnit.MILLISECONDS.sleep(1);
} catch (InterruptedException e) {
break;
}
}
System.out.println("I will be exiting. → " + new Date());
}
};
t.start();
TimeUnit.MINUTES.sleep(1);
System.out.println("System will be shutdown. → " + new Date());
t.interrupt();
}
}
I will start work → Wed May 31 19:07:27 CST 2023
System will be shutdown. → Wed May 31 19:08:27 CST 2023
I will be exiting. → Wed May 31 19:08:27 CST 2023
2.3、业务判断interrupt标识来决定是否退出
public class InterruptThreadExit {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread() {
@Override
public void run() {
System.out.println("I will start work. → " + new Date());
while (!isInterrupted()) {
//working.
}
System.out.println("I will be exiting. → " + new Date());
}
};
t.start();
TimeUnit.MINUTES.sleep(1);
System.out.println("System will be shutdown. → " + new Date());
t.interrupt();
}
}
I will start work. → Wed May 31 19:03:58 CST 2023
System will be shutdown. → Wed May 31 19:04:58 CST 2023
I will be exiting. → Wed May 31 19:04:58 CST 2023
2.3、使用volatile开关控制
由于线程的interrupt标识很有可能被擦除,或者逻辑单元中不会调用任何可中断方法,所以用volatile修饰的开关flag关闭线程也是一种常用的做法
public class FlagThreadExit {
static class MyTask extends Thread {
private volatile boolean closed = false;
@Override
public void run() {
System.out.println("I will start work. → " + new Date());
while (!closed && !isInterrupted()) {
//正在运行
}
System.out.println("I will be exiting. → " + new Date());
}
public void close() {
this.closed = true;
this.interrupt();
}
}
public static void main(String[] args) throws InterruptedException {
MyTask t = new MyTask();
t.start();
TimeUnit.MINUTES.sleep(1);
System.out.println("System will be shutdown. → " + new Date());
t.close();
}
}
I will start work. → Wed May 31 19:18:32 CST 2023
System will be shutdown. → Wed May 31 19:19:32 CST 2023
I will be exiting. → Wed May 31 19:19:32 CST 2023
废弃方法stop
JDK有一个Deprecated方法stop
[Q&A] stop为啥被废弃?
根据官网的描述,该方法在关闭线程时可能不会释放掉monitor的锁
,所以强烈建议不要使用该方法结束线程。所以JDK官方早已经不推荐使用,其在后面的版本中有可能会被移除。
进程假死
相信很多程序员都会遇到进程假死的情况,所谓假死就是进程虽然存在,但没有日志输出,程序不进行任何的作业,看起来就像死了一样,但事实上它是没有死的,程序之所以出现这样的情况,绝大部分的原因就是某个线程阻塞
了,或者线程出现了死锁
的情况。
-----------------------------------------------------------------------------读书笔记摘自书名:Java高并发编程详解:多线程与架构设计 作者:汪文君