JDK-8232676 : Thread.isInterrupted() always returns false after thread termination
  • Type: CSR
  • Component: hotspot
  • Sub-Component: runtime
  • Priority: P4
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 14
  • Submitted: 2019-10-21
  • Updated: 2019-10-29
  • Resolved: 2019-10-28
Related Reports
CSR :  
Description
Summary
-------

Update the `java.lang.Thread` specification in regards to interruption happening while a thread is not alive, to account for a new implementation that tracks the interrupted state at all times in the thread lifecycle.

Problem
-------

The specification for `java.lang.Thread::interrupt` allows for thread interruption to be a no-op if the thread is not alive:

>  Interrupting a thread that is not alive need not have any effect.

and the `Thread::interrupted` and `Thread::isInterrupted` method reflect the behaviour of the Hotspot VM by stating:

> A thread interruption ignored because a thread was not alive at the time of the interrupt will be reflected by this method returning false.

With the new implementation of the interrupted state an accurate record of the interrupt state of a thread is available at all times in the thread's lifecycle and so these parts of the specification are no longer correct.

Solution
--------

We can remove the statements about returning false; and we can add an implementation note that interruption while a thread is not alive is not ignored.

Specification
-------------

For `Thread::interrupt` add:

    * @implNote In this implementation, interruption of a thread that is not
    * alive still records the interrupt request was made and will report it
    * via {@link #interrupted} and {@link #isInterrupted()}.

For `Thread::interrupted` and `Thread::isInterrupted` delete:

     * <p>A thread interruption ignored because a thread was not alive
     * at the time of the interrupt will be reflected by this method
     * returning false.


Comments
Thanks Joe. I will update the terminology as suggested.
29-10-2019

Moving to Approved, but as I code view comment I suggest using wording like "In the JDK Reference Implementation, interruption of a thread..." That wording in using in other implNote tags in java.base. In other cases, the implNote tag in making a more constrained claim on a particular methods vs the platform.
28-10-2019

>> Martin: I want to keep this only about the JDK implementation. OK. >> If you would like that removed from the spec completely feel free to file a separate RFE and CSR for that. It would need buy-in from other VM providers. I'd like that to happen, but am not in a position to do the work. >> It's possible that JCK tests may need to be updated. Seems unlikely, since the old behavior was unhelpful, unpredictable, and unspecified. Future tests may end up taking advantage of the improved implementation. My own failed attempt to do so earlier is how we got here.
22-10-2019

Doug, Martin: thanks for the reviews. Martin: I want to keep this only about the JDK implementation. If you would like that removed from the spec completely feel free to file a separate RFE and CSR for that. It would need buy-in from other VM providers. [~dcubed] Thanks for the review. It's possible that JCK tests may need to be updated. Checking that is still on the to-do list.
21-10-2019

[~dholmes] - Will any JCK tests be affected by this change? (Just a sanity check query)
21-10-2019

This proposal is clear progress. The old behavior is surprising and not useful. I want to go a little further and also delete """Interrupting a thread that is not alive need not have any effect.""", i.e. making this a requirement of all VM implementations - predictability is in the spirit of Java - but that makes any progress less likely to happen.
21-10-2019

Seems fine to me. I cannot imagine any code that would depend on this.
21-10-2019

[~martin], [~dl]: please validate the above, provide comments if necessary or else add yourselves as reviewers. Thank you.
21-10-2019