java线程睡眠中断_Java线程中断的三种方法

多线程编程中耗时线程是很常见的情况,有时候我们不得不在一个线程中去终止另一个耗时线程。JDK并不推荐直接停止比如this.stop(),这会导致一些异常,比如锁未释放程序一直死锁。JDK推荐使用一个线程去通知耗时线程该结束线程了,耗时线程做退出前的回收处理然后自己结束线程。

自定义标志位终止

使用volatile 修饰的变量isExit控制线程的退出,这种方法需要不断及时判断isExit的值

public classFinishThreadLearn {public static void main(String[] args) throwsInterruptedException {

InterruptStopThread threadA= new InterruptStopThread("A");

threadA.start();

Thread.sleep(100);

System.out.println("终止线程");

threadA.isExit=true;

}

}class InterruptStopThread extendsThread {privateString name;public volatile boolean isExit = false;publicInterruptStopThread(String name) {this.name =name;

}

@Overridepublic voidrun() {super.run();while (!isExit) {

System.out.println("Thead:" + this.name + "正在运行");

}

System.out.println("线程结束运行");

}

}

输出结果可以看到,线程并未立即终止,结束循环体后才最终结束线程。

43d6a7ae0ba3354263c1535147bd3948.png

interrupt() 方式终止

正常执行的代码

public classFinishThreadLearn {public static void main(String[] args) throwsInterruptedException {

InterruptStopThread threadA= new InterruptStopThread("A");

threadA.start();

Thread.sleep(100);

System.out.println("终止线程");

threadA.interrupt();

}

}class InterruptStopThread extendsThread {privateString name;public volatile boolean isExit = false;publicInterruptStopThread(String name) {this.name =name;

}

@Overridepublic voidrun() {super.run();while (!isInterrupted()) {

System.out.println("Thead:" + this.name + "正在运行");

}

System.out.println("线程结束运行");

}

}

执行结果

05327e2f9f83d0ee063eb018275208a2.png

处于阻塞休眠的代码

这里有点不一样,interrupt()方式中断阻塞会把标志位清除并报出InterruptedException异常,所以要在catch的时候退出。

public classFinishThreadLearn {public static void main(String[] args) throwsInterruptedException {

InterruptStopThread threadA= new InterruptStopThread("A");

threadA.start();

Thread.sleep(2000);

System.out.println("终止线程");

threadA.interrupt();

}

}class InterruptStopThread extendsThread {privateString name;public volatile boolean isExit = false;publicInterruptStopThread(String name) {this.name =name;

}

@Overridepublic voidrun() {super.run();while (true) {

System.out.println("Thead:" + this.name + "正在运行");try{

Thread.sleep(1000);

}catch(InterruptedException e) {

e.printStackTrace();break;

}

}

System.out.println("线程结束运行");

}

}

执行结果

63dff75412366ddb03e9b0399ae4068c.png

stop()强行停止

public classFinishThreadLearn {public static void main(String[] args) throwsInterruptedException {

InterruptStopThread threadA= new InterruptStopThread("A");

threadA.start();

Thread.sleep(100);

System.out.println("终止线程");

threadA.stop();

}

}class InterruptStopThread extendsThread {privateString name;public volatile boolean isExit = false;publicInterruptStopThread(String name) {this.name =name;

}

@Overridepublic voidrun() {super.run();while (!isInterrupted()) {

System.out.println("Thead:" + this.name + "正在运行");

}

System.out.println("线程结束运行");

}

}

从执行结果可以看到,run方法并未完全执行完就结束了,所以这种方法不推荐使用

f9221dffe892ec3e5402bcf928bf945a.png

源码分析

Thread源码中使用了一个volatile修饰的标志位控制终止信号

public class Thread implementsRunnable {

.../*Interrupt state of the thread - read/written directly by JVM*/

private volatile booleaninterrupted;

...

}

interrupt方法会使标志位变为true

3aaaa13ed4a5c6683dd4e7cb195ad45d.png

判断线程是否终止JDK提供了两种方式

Thread.interrupted():测试当前线程是否已经中断this.isInterrupted():测试线程是否已经中断

this.isInterrupted()

调用isInterrupted会返回该标志位

ee28bd089d5d4185f42fa2548aef89a5.png

Thread.interrupted()

调用interrupted会返回该标志位,如果为ture会将标志位清空,且这个方法为静态方法

34222dce9f652220403266fa51e69164.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值