//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
public class DesignModuleDoubleParse {
public DesignModuleDoubleParse() {
}
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> {
while(true) {
Thread current = Thread.currentThread();
//如果线程被打断了
if (current.isInterrupted()) {
System.out.println("料理后事......");
return;
}
//如果线程没被打断
try {
Thread.sleep(500L);
System.out.println("监视内存使用情况......");
} catch (InterruptedException var2) {
current.interrupt();
var2.printStackTrace();
}
}
}, "第一个线程");
t1.start();
t1.join(6000L);
System.out.println("终止t1线程运行");
t1.interrupt();
}
}
这个设计模式用于优雅的结束线程。在线程被kill之前可以执行一些善后工作。
错误思路:用stop()方法直接结束线程。如果此线程正获得了锁,该锁将永远不被释放。