在Java中终止线程是一个比较温柔的过程,是让线程中的run方法执行完,被终止线程自己决定要不要终止,没有其他线程的干扰。
public class Demo7 {
public static void main(String[] args) {
Thread t = new Thread( () -> {
while(true) {
System.out.println("t 线程");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
t.start();
System.out.println("main 线程");
}
}
这一段代码中有两个线程,主线程main和t线程,其中main线程只打印一次,而t线程要打印无数次,若要终止t线程的打印,就要这样:
public class Demo7 {
public static void main(String[] args) {
Thread t = new Thread( () -> {
//先获取到线程的引用
Thread currentThread = Thread.currentThread();
while(!currentThread.isInterrupted()) {
System.out.println("t 线程");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
t.start();
t.interrupt();
System.out.println("main 线程");
}
}
先要获取到线程的引用
Thread currentThread = Thread.currentThread();
然后哪个线程调用了interrupt(),就会设置isInterrupted()的标志位,而isInterrputed()默认是false的。
但是如果我把t.interrput()写在System.out.println(“main 线程”)之后就会报错
这是因为在执行t.interrput()时,t线程还在sleep当中,此时t.interrput()不仅会设置标志位,还会将sleep给唤醒,而sleep被唤醒之后,又会清空刚才设置的标志位,抛出Runntime异常,又因为这个异常并未被catch,所以会出现上述情况,正确的做法是将 原来的throw删掉,直接写上return/break就好了。
这也就给了t线程更多的选择:
①:如果t线程想无视这个终止,就直接catch中啥也不做;
②:如果t线程想立即结束,就直接在catch中写上return/break;
③:如果t线程想稍后结束,就可以在catch中写上一些其他的逻辑(释放资源,清理数据,提交结果…),完了之后在写上return/break。