Is an "atomic" interrupt check possible in java?
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?
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.
只有一个调用可以清除中断标志,因此isInterrupted和queue.take()之间不会发生任何事情。
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).
链接地址: http://www.djcxy.com/p/34510.html上一篇: 在程序运行期间观看生成列表