If you use the following "interrupt" idiom 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, the interrupt cannot be ignored if the interrupt "arrives" between check Thread.currentThread().isInterrupted() and call queue.take() ? Is this not a validation than action operation? If so, is there any way to guarantee that the loop is left in any case if the thread is interrupted?
You can use polling with a timeout so that the cycle remains after the timeout , but is it possible to check the interrupted status and act on it atomically?
java multithreading concurrency interrupt
user140547
source share