Java中多线程的终止

在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。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值