Blocks :
|
|
CSR :
|
|
Duplicate :
|
|
Relates :
|
|
Relates :
|
|
Relates :
|
|
Relates :
|
JDK-8234771 :
|
I was surprised that interrupt status was not preserved on thread termination. E.g. as demonstrated by: ``` public class PostTerminationInterruptStatus { static int countInterrupted(Thread[] threads) { int count = 0; for (Thread thread : threads) if (thread.isInterrupted()) count++; return count; } public static void main(String[] args) throws Throwable { final int n = 3; final Thread[] threads = new Thread[n]; for (int i = 0; i < n; i++) { threads[i] = new Thread(() -> Thread.currentThread().interrupt()); threads[i].start(); } System.out.println("" + countInterrupted(threads)); for (Thread thread : threads) thread.join(); System.out.println("" + countInterrupted(threads)); } } ``` The spec for Thread.interrupt and Thread.isInterrupted say that Thread.interrupt need not have any effect when the target thread has terminated BUT are silent on the question of what happens when Thread.isInterrupted is called when the target thread has terminated. Which suggests that the interrupt status needs to be preserved. The behavior of Thread is more understandable if we are aware of the implementation maintaining VM-internal state that contains the interrupt status, and that state is discarded on termination. Currently there is no non-intrusive way of asking whether a thread terminated with the interrupt status set. JDK developer lore is that the interrupt status was once a field in the java-level Thread class. I think it should be restored.
|