java线程中断原理

稍有java基础的同学都知道,在java中创建并启动一个线程比较容易,而线程中断的难度更高一些,并且使用的场景也相对较少。

interrupt()

中断某一个线程需要调用该线程对象的interrupt方法。

public class Demo {
    public static void main(String[] args) {
        Thread thread = new Thread(new MyTask());
        thread.start();
        try {
            Thread.sleep(1000);
        } catch (Exception ex) {
        }
        thread.interrupt();
    }
    static class MyTask implements Runnable {
        @Override
        public void run() {
            while (true) {
                System.out.println(Thread.currentThread() + " is running...");
            }
        }
    }
}

运行代码会发现,即使在主线程中执行目标线程的interrupt()方法,但目标线程并没有停止执行。这正是interrupt机制设计的特别之处,当主线程发起目标线程中断的命令后,目标线程并不会立即放弃线程的执行权。

中断标志位

java interrupt中断机制是当主线程向目标线程发起interrupt中断命令后,目标线程的中断标志位被置为true,目标线程通过查询中断标志位自行决定是否停止当前线程的执行。
这便解释了上面的代码中,目标线程的中断标志位虽然置为true,但由于并没有主动采取线程停止的操作,所以线程依然处于Running状态。
查询线程中断标志位的方法有两种:isInterrupted()和interrupted(),下面分别介绍二者的区别。

isInterrupted()与interrupted()

public static boolean interrupted() {
    return currentThread().isInterrupted(true);
}

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

private native boolean isInterrupted(boolean ClearInterrupted);

直接看这两个方法的源码,interrupted()是静态方法而isInterrupted()是实例方法,他们的实现都是调用同一个native方法。主要的区别是他们的形参ClearInterrupted传的不一样。interrupted()在返回中断标志位后会清除标志位,isInterrupted()则不清除中断标志位。
接下来改造前面的代码,实现线程中断效果:

public class Demo {

    public static void main(String[] args) {
        Thread thread = new Thread(new MyTask());
        thread.start();
        try {
            Thread.sleep(100);
        } catch (Exception ex) {
        }
        thread.interrupt();
    }

    static class MyTask implements Runnable {
        @Override
        public void run() {
            while (true) {
                if (Thread.interrupted()) {
                    break;
                }
                System.out.println(Thread.currentThread() + " is running...");
            }
            System.out.println("当前中断标志位状态:" + Thread.currentThread().isInterrupted());
        }
    }
}
10271250-a6253e26cc442bff.png
image.png

InterruptedException

调用Thread.sleep()时都需要捕获InterruptedException异常。这个异常的作用是什么?
如果目标线程正在执行阻塞方法(sleep、join),而其他线程恰好调用了目标线程的interrupt方法试图中断目标线程,sleep、join这类阻塞方法会检查线程的中断标志位,并抛出InterruptedException异常。

阻塞方法为何抛出InterruptedException

@Override
public void run() {
    while (true) {
        if (Thread.interrupted()) {
            break;
        }
        // point 1 : 阻塞方法前逻辑
        try {
            // point 2 : 阻塞方法中
            Thread.sleep(10*000*000);
        } catch(InterruptedException ex) {
            // 执行清除逻辑
        }
        // point 3 :阻塞方法后逻辑
    }
}

前文提到过,interrupt的线程中断机制是由发起线程将目标线程的中断标志位置为true,至于是否执行线程的中断由目标线程决定。
以上面代码为例,如果目标线程正在执行sleep方法而线程阻塞,必须在10000000时间完成且并执行完后续逻辑,直至循环里下次interrupted()判断后才能中断线程。显然这不是我们希望看到的,所以阻塞方法会判断中断标志位,一旦出现中断的命令就会抛出异常,直接终止阻塞逻辑。

InterruptedException清空中断标志位

抛出InterruptedException异常也会清除中断标志位,如果想要继续保留中断标志位的状态,可以手动触发中断标志,代码如下:

try {
  Thread.sleep(100);
} catch (InterruptedException ex) {
  Thread.currentThread().interrupt(); 
  throw new RuntimeException(ex);
}

总结

要想理解java线程中断的原理,重点就是要掌握中断标志位的使用细节,其他的逻辑都是围绕中断标志位设计。

猜你喜欢

转载自blog.csdn.net/weixin_33692284/article/details/86872844