当前位置: 代码迷 >> 综合 >> java-日常练习-多线程-停止线程方法介绍(stop,interrupt,结合while使用退出标签)
  详细解决方案

java-日常练习-多线程-停止线程方法介绍(stop,interrupt,结合while使用退出标签)

热度:54   发布时间:2023-10-24 17:05:52.0

java 三种方法停止线程
1.姿势不对interrupt 永远不能停止线程运行

package a;
class  Test extends  Thread{@Overridepublic 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 这样暴力 不可控的方法。
能停止线程的方法

  1. for循环根据线程标签状态停止
class  Test extends  Thread{@Overridepublic 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{@Overridepublic 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{@Overridepublic 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");}
}

在这里插入图片描述
总结:

在这里插入图片描述

  相关解决方案