JAVA如何优雅地停止一个线程_React java 如何优雅的停止一个线程 _好机友

本文探讨了多线程编程中两种常见的线程终止方法:使用标志位和Thread.currentThread().isInterrupted()。通过实例分析了它们的优缺点,特别是在无限循环场景下,isInterrupted()在优雅停止线程方面的优势,以及潜在的限制。
摘要由CSDN通过智能技术生成

写过多线程的童鞋,可能都会遇到一个问题,那就是线程如何优雅的停止。这里主要介绍两个办法:标志位 和 thread.isInterrupted。

1.通过 标志位 来终止线程private static class Runner implements Runnable{ private long i; private (volatile) boolean running =true; @Override public void run() { System.out.println("current Thread Name:"+Thread.currentThread().getName()); while (running ){ i++; try { TimeUnit.SECONDS.sleep(10); } catch (InterruptedException e) { } } System.out.println("Count i= "+i); System.out.println("current Thread Name:"+Thread.currentThread().getName()); } public void cancel(){ running =false; System.out.println("running=false"); } }

此程序会在 running=false 之后,继续等待 10s ,通过 标识位 并不能阻止 sleep wait 等继续执行。 所以此方法不是特别优雅

2.通过 Thread.currentThread().isInterrupted() 来判断public class Shutdoown { public static void main(String[] args) { Runner one = new Runner(); Thread countThread = new Thread(one, "CountThread"); countThread.start(); try { //保证线程已启动 TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { } countThread.interrupt(); } private static class Runner implements Runnable { private long i; private volatile boolean running = true; @Override public void run() { System.out.println("current Thread Name start:" + Thread.currentThread().getName()); while (!Thread.currentThread().isInterrupted()) { System.out.println("=== " + Thread.currentThread().isInterrupted()); i++; try { TimeUnit.SECONDS.sleep(5); } catch (InterruptedException e) { Thread.currentThread().interrupt(); System.out.println("error=== " + e); } } System.out.println("Count i= " + i); System.out.println("current Thread Name stop:" + Thread.currentThread().getName()); } }

thread 被中断之后,就可以无视 sleep wait 等方法,马上停止,乍一看,还是蛮优雅的,推荐使用。

可如果我把 sleep 换乘 while(true)会怎么样?通过 Thread.currentThread().isInterrupted() 来判断-while(true) 版本public class Shutdoown { public static void main(String[] args) { Runner one = new Runner(); Thread countThread = new Thread(one, "CountThread"); countThread.start(); try { //保证线程已启动 TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { } countThread.interrupt(); } private static class Runner implements Runnable { private long i; private volatile boolean running = true; @Override public void run() { System.out.println("current Thread Name start:" + Thread.currentThread().getName()); while (!Thread.currentThread().isInterrupted()) { System.out.println("=== " + Thread.currentThread().isInterrupted()); i++; while(true){ } } System.out.println("Count i= " + i); System.out.println("current Thread Name stop:" + Thread.currentThread().getName()); } }

不要意思,除非主动 kill,不然 线程 永远停止不了。

总结: 显然,通过 Thread.currentThread().isInterrupted() 来停止线程,显然要比标示位优雅一些,但要防止条件判断永远无法生效。

相关栏目:

React

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值