Interrupt

826 阅读2分钟

在JDK之前的版本中可以通过stop、suspend来终止、中断线程的指向,不过目前这些方法已经不建议使用了,那如果希望在执行到某一条件时中断线程的执行,应该怎么做呢?目前推荐的方式是通过interrupt来实现。

1 源码分析

1.1 interrupt()

Thread#interrupt()的作用其实也不是中断线程,而是通知线程应该中断了,具体来说,当对一个线程调用 interrupt() 时:

  • 如果线程处于被阻塞状态(例如处于sleep, wait, join 等状态),那么线程将立即退出被阻塞状态,并抛出一个InterruptedException异常,仅此而已。
  • 如果线程处于正常活动状态,那么会将该线程的中断标志设置为 true,仅此而已。被设置中断标志的线程将继续正常运行,不受影响。 interrupt() 并不能真正的中断线程,需要被调用的线程自己进行配合才行,在正常运行任务时,经常检查本线程的中断标志位,如果被设置了中断标志就自行停止线程。具体到底中断还是继续运行,应该由被通知的线程自己处理。
public void interrupt() {
    if (this != Thread.currentThread())
        checkAccess();

    synchronized (blockerLock) {
        Interruptible b = blocker;
        if (b != null) {
            interrupt0();           // Just to set the interrupt flag
            b.interrupt(this);
            return;
        }
    }
    interrupt0();  // Just to set the interrupt flag
}

1.2 interrupted()

检查当前中断标识(即查看当前中断信号是true还是false),并清除中断信号。一般处理过中断以后使用此方法。

public static boolean interrupted() {
    return currentThread().isInterrupted(true);
}
/**
 * Tests if some Thread has been interrupted.  The interrupted state
 * is reset or not based on the value of ClearInterrupted that is
 * passed.
 */
private native boolean isInterrupted(boolean ClearInterrupted);

1.3 isInterrupted()

检查当前中断标识(即查看当前中断信号是true还是false)

    public boolean isInterrupted() {
        return isInterrupted(false);
    }

2 示例

以下示例展示了通过isInterrupted()、interrupt()如何终止执行中的线程,如下所示代码中中创建了两个线程(thread1、thread2),thread1每隔500ms数数一次,thread2休眠一段时间以后中断thread1,thread1将会终止。

public static void main(String[] args) {
    Thread thread1 = new Thread(() -> {
        try {
            int i = 0;
            while (!Thread.currentThread().isInterrupted()) {
                Thread.sleep(500);
                System.out.println("couting... i=" + i);
                i++;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            System.out.println("thread1 interrupted");
        }
    });
    Thread thread2 = new Thread(() -> {
        Random random = new Random();
        try {
            long time = random.nextInt(3000) + 1000;
            Thread.sleep(time);
            System.out.println("interrupt thread1");
            thread1.interrupt();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    });
    thread1.start();
    thread2.start();
}