我们可以有虚假的干扰吗? [英] Can we have spurious interrupts?

查看:74
本文介绍了我们可以有虚假的干扰吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个任务轮询器,每分钟查找一次任务.看起来像这样:

I am creating a task poller which looks for tasks every minute. It looks like this:

public class Poller {
    private final ExecutorService e = Executors.newSingleThreadExecutor();

    public void start() {
        e.submit(() -> {
            while (!Thread.currentThread().isInterrupted()) {
                final Task task = manager.getTask();

                if (task != null) {
                    // ...
                } else {
                    try {
                        TimeUnit.MINUTES.sleep(1);
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    }
                }
            }
        });
    }

    public void stop() {
        e.shutdownNow();
    }
}

当我想停止轮询程序时,我执行stop().这会在正在运行的线程上触发一个中断,这将停止轮询器.

When I want to stop the poller process, I exucute stop(). This triggers an interrupt on the running thread, which will stop the poller.

这很好.但是,我想知道此线程是否有可能被其他人(最有可能是JVM)中断.在这种情况下,轮询程序将退出,而不应退出.

This works fine. However, I am wondering if there is a possibility that this thread will be interrupted by someone else, most likely the JVM. In that case, the poller will quit while it shouldn't.

我知道我们可能会有虚假的唤醒,所以我们必须用while (condition) { ... }而不是if (condition) { ... }来保护wait(). JVM是否也可能无缘无故地中断正在进行的线程?

I know we can have spurious wakeups, so we have to guard a wait() with a while (condition) { ... } instead of if (condition) { ... }. Can it also occur that the JVM will interrupt an ongoing thread for no good reason?

在这种情况下,我应该引入一个volatile boolean并检查它而不是while (!Thread.currentThread().isInterrupted())?

In that case, I should introduce a volatile boolean and check on that instead of while (!Thread.currentThread().isInterrupted())?

推荐答案

是的,应该引入volatile boolean来控制循环.不仅因为可能会产生虚假中断,还因为它使代码更具可读性.

Yes, you should be introduce a volatile boolean to control the loop. Not just because of the possibility of a spurious interrupt, but because it makes the code more readable.

您没有通过保存" boolean并使用线程的中断状态来控制循环来实现聪明的优化,但是对于类的工作机制却不那么清楚.

You're not achieving a clever optimization by "saving" a boolean and using the interrupt status of the thread to control the loop, you're making it less obvious as to what the working mechanism of the class is.

我并没有真正回答核心问题(将其与唤醒功能混在一起).我们知道,虚假唤醒可以并且确实发生,这就是为什么我们使用保护子句的原因/循环检查条件是否确实满足.

I wasn't really answering the core question (mixed it up with wakeups). As we know, spurious wakeups can and do happen, which is why we use guard clauses/loops to check that a condition has really been satisfied.

虚假中断不是问题,这意味着Java生态系统内部总会有一个线程中断另一个线程(与唤醒不同,原因是外部的).中断随机线程不是Java平台本身要做的事情,但是您可以编写一个线程,该线程只是随机选择其他线程并尝试中断它们.但是,这不是虚假的中断,而是恶意中断.

Spurious interrupts aren't a thing, meaning there's always one thread interrupting another inside the Java ecosystem (unlike with wakeups, where the reason is external). Interrupting random threads is not something that the Java platform does by itself, but you could write a thread that just randomly selects other threads and tries to interrupt them. However that wouldn't be a spurious interrupt, that would be a malicious interrupt.

这篇关于我们可以有虚假的干扰吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆