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

Thread.isInterrupted() always returns false after thread termination

    Details

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

      Description

      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.

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                • Created:
                  Updated:
                  Resolved: