Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8229516

Thread.isInterrupted() always returns false after thread termination


    • Type: Enhancement
    • Status: Resolved
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: 14
    • Fix Version/s: 14
    • Component/s: hotspot
    • Subcomponent:
    • Resolved In Build:


      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())
              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());
              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.


          Issue Links



              • Assignee:
                dholmes David Holmes
                martin Martin Buchholz
              • Votes:
                0 Vote for this issue
                4 Start watching this issue


                • Created: