java-日常练习-多线程-停止线程方法介绍(stop,interrupt,结合while使用退出标签)
2021/10/15 20:16:42
本文主要是介绍java-日常练习-多线程-停止线程方法介绍(stop,interrupt,结合while使用退出标签),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1.姿势不对interrupt 永远不能停止线程运行
package a; class Test extends Thread{ @Override public void run() { super.run(); for (int i = 0; i <1000 ; i++) { System.out.println("i="+i); try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class Sender extends Thread { public static void main(String[] args) throws InterruptedException { Test a=new Test(); a.start(); a.interrupt(); Thread.sleep(100); System.out.println("已经让你停止了"); } }
从打印结果来看, 并没有停止呀, 大家知道什么原因吗?
诊断没有停止的原因
先看诊断停止的方法的区别
public class Sender extends Thread { public static void main(String[] args) throws InterruptedException { Thread.currentThread().interrupt(); System.out.println(Thread.currentThread().isInterrupted()); System.out.println(Thread.currentThread().isInterrupted()); System.out.println(Thread.interrupted()); System.out.println(Thread.interrupted()); System.out.println("已经让你停止了"); } }
打印的结果:
true true true false 已经让你停止了
大家看到调用了interrupt方法,还是没有停止主线程,所以当调用了 interrupt方法 只是给线程打个flag, 根据flag 进行判断后边的操作,这样更灵活。所以抛弃了stop 这样暴力 不可控的方法。
能停止线程的方法
- for循环根据线程标签状态停止
class Test extends Thread{ @Override public void run() { super.run(); for (int i = 0; i <100000 ; i++) { if(this.isInterrupted()){ System.out.println("停止成功了"); break; } System.out.println("i="+(i+1)); } System.out.println("for循环结束了"); } } public class Sender extends Thread { public static void main(String[] args) throws InterruptedException { Test a= new Test(); a.start(); Thread.sleep(40); a.interrupt(); System.out.println("end"); } }
打印结果显示已经中断了
上边的方法缺点: 如果for 或者while 循环下边还有语句,那么程序还会继续运行,上边的日志 for循环结束了就是for循环后边的代码 。
解决方法 直接用 return 尝试下,直接修改break 变成return ,打印结果如下:
虽然可以实现立即中断的效果,大家有没有考虑过, 这样是否太暴力了, 比如我想中断后想处理下数据作为收尾怎么办,可以用异常中断方法。
异常中断方法
请看下边例子 :
package a; class Test extends Thread{ @Override public void run() { super.run(); try { for (int i = 0; i <100000 ; i++) { if(this.isInterrupted()){ System.out.println("停止成功了"); throw new InterruptedException(); } System.out.println("i="+(i+1)); } System.out.println("for循环结束了"); } catch (InterruptedException e) { System.out.println("已经触发了异常,我开始清理工作中......."); } } } public class Sender extends Thread { public static void main(String[] args) throws InterruptedException { Test a= new Test(); a.start(); Thread.sleep(40); a.interrupt(); System.out.println("end"); } }
打印结果:
满足了我们所有的需求。
在sleep 状态下停止线程会报异常
class Test extends Thread{ @Override public void run() { super.run(); try { for (int i = 0; i <100000 ; i++) { Thread.sleep(1000); if(this.isInterrupted()){ System.out.println("停止成功了"); throw new InterruptedException(); } System.out.println("i="+(i+1)); } System.out.println("for循环结束了"); } catch (InterruptedException e) { System.out.println("已经触发了异常,我开始清理工作中......."); e.printStackTrace(); } } } public class Sender extends Thread { public static void main(String[] args) throws InterruptedException { Test a= new Test(); a.start(); Thread.sleep(40); a.interrupt(); System.out.println("end"); } }
总结:
这篇关于java-日常练习-多线程-停止线程方法介绍(stop,interrupt,结合while使用退出标签)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)
- 2024-05-30【Java】百万数据excel导出功能如何实现
- 2024-05-30我们小公司,哪像华为一样,用得上IPD(集成产品开发)?
- 2024-05-30java excel上传--poi
- 2024-05-30安装笔记本应用商店的pycharm,再安排pandas等模块,说是没有打包工具?
- 2024-05-29java11新特性
- 2024-05-29哪些无用敏捷指标正在破坏敏捷转型?
- 2024-05-29鸿蒙原生应用再新丁!新华社 入局鸿蒙
- 2024-05-29设计模式 之 迭代器模式(Iterator)