线程中断机制概述

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第26天,点击查看活动详情

Java 没有提供一种安全,直接的方式来停止某个线程,而是提供了中断机制。中断机制是一种协作机制,也就是说通常中断并不能直接终止另一个线程,而需要中断的线程处理。 被中断的线程拥有完全的自主权,它既可以选择立即停止,也可以选择一段时候后停止,也可以选择不停止。

Thread 中可实现线程中断的方法

  • interrupt(): 将线程的中断标志位设置为 true , 不会停止线程。
  • isInterrupted():判断当前线程的中断标志位是否为 true , 不会清除中断标志位。
  • Thread.interrupted(): 判断当前线程的中断标志是否为true, 清除中断标志位,重置为 false。

一个简单的中断使用的示例代码:

public class ThreadInterruptTest {

    static int i = 0;

    public static void main(String[] args) {
        System.out.println("begin");
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    i++;
                    System.out.println(i);
                    //Thread.interrupted()  清除中断标志位
                    //Thread.currentThread().isInterrupted() 不会清除中断标志位
                    if (Thread.currentThread().isInterrupted()) {
                        System.out.println("=========");
                    }
                    if (i == 10) {
                        break;
                    }

                }
            }
        });

        t1.start();
        //不会停止线程t1,只会设置一个中断标志位 flag=true
        t1.interrupt();
    }
}
复制代码

利用中断机制停止线程

举个例子

while (!Thread.currentThread().isInterrupted() && more work to do) {
    do more work
复制代码

通过中断来停止正在执行的线程:

public class StopThread implements Runnable {

    @Override
    public void run() {
        int count = 0;
        while (!Thread.currentThread().isInterrupted() && count < 1000) {
            System.out.println("count = " + count++);
        }
        System.out.println("线程停止: stop thread");
    }

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new StopThread());
        thread.start();
        Thread.sleep(5);
        thread.interrupt();
    }
}
复制代码

输出结果: image.png 注意:使用中断机制时一定要注意是否存在中断标志位被清除的情况

sleep 和 wait 和线程中断

我们修改一下上面的 run 方法:

@Override
public void run() {
    int count = 0;
    while (!Thread.currentThread().isInterrupted() && count < 1000) {
        System.out.println("count = " + count++);

        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    System.out.println("线程停止: stop thread");
}
复制代码

打印结果如下: image.png 在执行到 count == 99 的时候 image.png

处于休眠中的线程被中断,线程是可以感受到中断信号的,并且会抛出一个 InterruptedException 异常,同时清除中断信号,将中断标记位设置成 false。 这样就会导致while条件Thread.currentThread().isInterrupted()为false,程序会在不满足count < 1000这个条件时退出。如果不在catch中重新手动添加中断信号,不做任何处理,就会屏蔽中断请求,有可能导致线程无法正确停止。

正确的设置方式: image.png

总结

  • sleep() 可以被中断 并且抛出中断异常:sleep interrupted, 清除中断标志位。
  • wait() 可以被中断, 并且抛出中断异常:InterruptedException, 清除中断标志位。
  • 如果要在 sleep()wait() 方法中断有效,需要捕获异常重新设置中断位。

猜你喜欢

转载自juejin.im/post/7090922713615630372