前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java中的中断机制

Java中的中断机制

作者头像
吴就业
发布2021-12-13 14:20:13
9650
发布2021-12-13 14:20:13
举报
文章被收录于专栏:Java艺术Java艺术

在Java中,用于终止一个正在运行中的线程,并非调用stop方法,而是自行设置一个标志位,在安全点检测标志位,决定是否退出,但也可能会因为线程被挂起,无法走到标志位。因此,Java线程提供了中断机制,Thread类提供了中断线程执行的调用方法:interrupt,用于中断因线程挂起的等待,调用interrupt方法后,线程会被唤醒,待下次cpu调度就会继续执行中断后的代码

我们经常会调用Thread#sleep、Object#wait、Queue#poll等方法,并要求我们处理InterruptedException异常。 那么,抛出InterruptedException后,线程会终止吗?

如果不捕获InterruptedException,那么线程就会因为异常终止,是因为异常终止,并不是因为被中断。如果捕获了InterruptedException,那么线程就不会终止。

中断,其实只是jvm用于唤醒因锁竞争、I/O操作、休眠等待被挂起的线程,并设置一个中断标志,我们可以利用这个标志去做一些处理。比如,当我们发送消息给远程服务器,并休眠等待结果时,如果线程被唤醒,并设置了中断标志,此时我们可以知道,并非等到结果被唤醒的,而是被中断唤醒的,可以决定是继续等待结果,还是放弃等待。

xxl-job提供取消任务操作,而任何运行中的线程,都只能利用中断机制去结束线程任务,所以我们想要任务支持被取消,那么在写定时任务时,一定要考虑清楚,是不是应该捕获InterruptedException,如何利用中断标志结束任务,否则将会导致任务无法被取消。

我们来看个案例:

代码语言:javascript
复制
@Test
public void test() {
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    Future<?> future = executorService.submit(() -> {
        while (true) {
            System.out.println( "rung....." );
            ThreadUtils.sleep(1000);
        }
    });
    ThreadUtils.sleep(1000);
    future.cancel(true);
    try {
        future.get();
    } catch (InterruptedException | CancellationException | ExecutionException e) {
        e.printStackTrace();
    }
    ThreadUtils.sleep(1000 * 60);
}

此案例创建了只有一个线程的线程池,提交了一个死循序任务,该任务只调用ThreadUtils.sleep方法进入休眠。平常我们调用Thread.sleep方法都要求是否捕获中断异常,很多时候我们都会嫌弃麻烦,就用一个工具类提供sleep方法,然后将中断异常捕获,如ThreadUtils:

代码语言:javascript
复制
public class ThreadUtils {
    public static void sleep(long millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException ignored) {
        }
    }
}

此案例中,由于我们捕获了中断异常,因此这会导致任务并不会被终止,只是当我们调用future的get方法时会抛出CancellationException异常,如下图所示。

任务依然在运行中......

因此,在实际开发中,如果我们开发的Job也是如此,将会导致Job无法被中断取消,直至Job执行完成或者重启。在开发Job时,应当合理考虑是否要捕获中断异常。

如果我们希望案例中的任务能够被终止,我们可以这样处理:

代码语言:javascript
复制
@Test
public void test() {
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    Future<?> future = executorService.submit(() -> {
        while (true) {
            System.out.println( "rung....." );
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                System.err.println( "interrupted" );
                return; // 退出死循环
            }
        }
    });
    ThreadUtils.sleep(1000);
    future.cancel(true);
    try {
        future.get();
    } catch (InterruptedException | CancellationException | ExecutionException e) {
        e.printStackTrace();
    }
    ThreadUtils.sleep(1000 * 60);
}

关于Thread的interrupt方法,注释描述的大致意思如下:

  • 如果被中断的线程,当前是调用Object#wait、Thread#join、Thread#sleep方法,将收到InterruptedException,并且会清除中断标志;
  • 如果此线程在I/O操作中(指java nio)被阻塞,调用interrupt方法通道将被关闭,线程将收到一个ClosedByInterruptException,并且会设置中断标志;
  • ....

怎么理解中断标志呢?

“如果被中断的线程,当前是调用Object#wait、Thread#join、Thread#sleep方法,将收到InterruptedException,并且会清除中断标志”,案例中的代码正好符合这点,如果我们将案例代码改为如下:

代码语言:javascript
复制
@Test
public void test() {
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    Future<?> future = executorService.submit(() -> {
        while (!Thread.interrupted()) {
            System.out.println( "rung....." );
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                System.err.println( "interrupted" );
            }
        }
    });
    ThreadUtils.sleep(1000);
    future.cancel(true);
    try {
        future.get();
    } catch (InterruptedException | CancellationException | ExecutionException e) {
        e.printStackTrace();
    }
    ThreadUtils.sleep(1000 * 60);
}

执行这段代码你会发现,死循环根本没有退出,正是因为Thread#sleep方法被中断,JVM并不会设置中断标志,只是抛出InterruptedException异常。

其它情况下,JVM只会设置中断标志,并不会抛出InterruptedException。如果我们不处理中断信号,那么中断信号并不会影响程序的继续执行。

代码语言:javascript
复制
@Test
public void test2() {
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    Future<?> future = executorService.submit(() -> {
        int number = 0;
        while (!Thread.interrupted()) {
            number++;
        }
        System.out.println(number);
    });
    ThreadUtils.sleep(1000);
    future.cancel(true);
    try {
        future.get();
    } catch (InterruptedException | CancellationException | ExecutionException e) {
        e.printStackTrace();
    }
    ThreadUtils.sleep(1000 * 60);
}

此案例并没有I/O操作导致的阻塞,因为调用中断方法后,线程只是设置了中断标志,我们用中断标志作为循序的退出条件,运行此案例,我们将看到,线程中断后,任务终止。反之,如果我们不处理中断标志,那么就等着IDEA进程卡掉吧。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-12-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Java艺术 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档