java 强制停止线程,Java 停止线程

1概述

在Java中有以下3种方法可以终止正在运行的线程:

1)当run方法完成后线程终止。

2)使用stop方法强行终止线程,但是不推荐使用这个方法,因为stop和suspend及resume一样,都是作废过期的方法,使用它们可能产生不可预料的结果。

3)使用interrupt方法中断线程。

2 stop()方法停止线程

try {

MyThread thread = new MyThread();

thread.start();

Thread.sleep(8000);

thread.stop();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

调用stop()方法时会抛出java.lang.ThreadDeath异常,但在通常的情况下,此异常不需要显式地捕捉。

调用stop()方法后线程会立即停止

问题

方法stop()已经被作废,因为如果强制让线程停止则有可能使一些清理性的工作得不到完成。

另外一个情况就是对锁定的对象进行了“解锁”,导致数据得不到同步的处理,出现数据不一致的问题。

3 调用interrupt()方法来停止线程

设置线程的中断状态

interrupt()方法的使用效果并不像for+break语句那样,马上就停止循环。

调用interrupt()方法仅仅是在当前线程中打了一个停止的标记,并不是真的停止线程。

3.1 this.interrupted() VS this.isInterrupted()

this.interrupted():测试当前线程是否已经是中断状态,执行后具有将状态标志置清除为false的功能。

this.isInterrupted():测试线程Thread对象是否已经是中断状态,但不清除状态标志。

3.2 interrupt方法配合抛出异常停止线程

建议使用“抛异常”的方法来实现线程的停止,因为在catch块中还可以将异常向上抛,使线程停止的事件得以传播。

【Java 多线程】Java中主线程如何捕获子线程抛出的异常

public class MyThread extends Thread {

@Override

public void run() {

super.run();

try {

for (int i = 0; i < 500000; i++) {

if (this.interrupted()) {

System.out.println("已经是停止状态了!我要退出了!");

throw new InterruptedException();

}

System.out.println("i=" + (i + 1));

}

System.out.println("我在for下面");

} catch (InterruptedException e) {

System.out.println("进MyThread.java类run方法中的catch了!");

e.printStackTrace();

}

}

}

public class Run {

public static void main(String[] args) {

try {

MyThread thread = new MyThread();

thread.start();

Thread.sleep(2000);

thread.interrupt();

} catch (InterruptedException e) {

System.out.println("main catch");

e.printStackTrace();

}

System.out.println("end!");

}

}

c986e7383784

qq_pic_merged_1534415736719.jpg

3.3 interrupt方法 配合 return 停止线程

程序正常执行完成,线程退出。

public class MyThread extends Thread {

@Override

public void run() {

while (true) {

if (this.isInterrupted()) {

System.out.println("停止了!");

return;

}

System.out.println("timer=" + System.currentTimeMillis());

}

}

}

public class Run {

public static void main(String[] args) throws InterruptedException {

MyThread t=new MyThread();

t.start();

Thread.sleep(2000);

t.interrupt();

}

}

c986e7383784

qq_pic_merged_1534415883582.jpg

3.4 注意

如果线程在调用 Object 类的 wait()、wait(long) 或 wait(long, int) 方法,或者该类的 join()、join(long)、join(long, int)、sleep(long) 或 sleep(long, int) 方法过程中受阻,则其中断状态将被清除,它还将收到一个 InterruptedException。

如果该线程在可中断的通道上的 I/O 操作中受阻,则该通道将被关闭,该线程的中断状态将被设置并且该线程将收到一个ClosedByInterruptException。

参考

《java多线程编程核心技术》

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值