Is an “atomic” interrupt check possible in java?

别来无恙 提交于 2019-12-23 08:44:32

问题


If using following "idiom" with interruption in Java, for example from this answer.

while (!Thread.currentThread().isInterrupted()) {
    try {
        Object value = queue.take();
        handle(value);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
}

Where take is a blocking operation, can an interrupt not be ignored for the time being if an interrupt "arrives" between the check of Thread.currentThread().isInterrupted() and the call queue.take()? Is this not a "check-than-act" operation? If so, can it somehow be guaranteed that the loop is left in any case if the thread is interrupted?

It is possible to use poll with a timeout so that the loop is left after the timeout, but is it possible to check the interrupted status and act on it atomically?


回答1:


I would swap the try/catch and while loop:

try {
  while (true) {
    Object value = queue.take();
    handle(value);
  }
} catch (InterruptedException e) {
  Thread.currentThread().interrupt();
}

The take() operation will throw an InterruptedException immediately if the thread is interrupted, and at the same time break out of the while loop.




回答2:


Only a call could clear then interrupted flag so nothing could happend between isInterrupted and queue.take().




回答3:


but is it possible to check the interrupted status and act on it atomically

Well - I don't know what you mean "atomically" here. May we assume you want something like onInterrupt(...)?

Interrupts are meant to "interrupt" the thread, so all default I/O operations throw an InterruptedException you can catch or check. It give the threads opportunity to stop gracefully closing / releasing any locked resources.

As event handling you may want to implement a Cancellable task, where you could handle your own cancel events (well, not the default JRE interrupts).



来源:https://stackoverflow.com/questions/37742304/is-an-atomic-interrupt-check-possible-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!