JDK-8290388 : 5.7: Clarify the definition of JVM termination
  • Type: Enhancement
  • Component: specification
  • Sub-Component: vm
  • Affected Version: 19
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2022-07-15
  • Updated: 2022-12-13
  • Resolved: 2022-12-13
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 20
20Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Description
JDK-8290196 significantly clarifies the conditions and timing of exit for a Java program (JLS 12.8). The goal there is to describe the experience of threads in the program -- which, significantly, includes the execution of shutdown hooks -- and *not* the actions of the JVM or the launcher that creates/terminates the JVM. By contrast, 5.7 should focus on the experience of the JVM. The API spec for Runtime.exit leads one to think that the JVM is mandated to start shutdown hooks, so 5.7 should reflect that. And, if the API spec develops tighter language around the JVM's "shutdown sequence", 5.7 should defer to it.
Comments
In the single opening paragraph of JVMS 2.5, it would be ideal if the two occurrences of "exits" ("when the Java Virtual Machine exits", "when the thread exits") could become "terminates". This aligns with the terminology above for 5.7.
13-12-2022

Revised section below. * 5.7 Java Virtual Machine Termination The Java Virtual Machine _terminates_ when one of the following situations has occurred: 1. A thread invoked System.exit or Runtime.exit, and all of the shutdown hooks which consequently were started by the Java Virtual Machine, if any, have terminated. 2. A thread invoked Runtime::halt. (No shutdown hooks are started in this situation.) 3. The Java Virtual Machine implementation recognized an external event as requesting termination of the Java Virtual Machine, and all of the shutdown hooks which consequently were started by the Java Virtual Machine, if any, have terminated. The nature of the event is outside the scope of this specification, but is necessarily something that a Java Virtual Machine implementation can handle reliably. An example is receiving a signal from the operating system. 4. An external event occurred that the Java Virtual Machine implementation cannot handle. (No shutdown hooks are started in this situation.) The nature of the event is outside the scope of this specification, but is necessarily something that a Java Virtual Machine implementation cannot recognize or recover from in any way. Examples include a fatal error occurring in the process running the implementation, or power being removed from the computer running the implementation. A thread _terminates_ if either (i) its `run` method completes normally, or (ii) its `run` method completes abruptly and the relevant uncaught exception handler (JLS 11.3) completes normally or abruptly. With no code left to run, the thread has completed execution and therefore has no current method (2.5.1). Upon JVM termination, any daemon or non-daemon thread that has not yet terminated will execute no further Java code. Its current method does not complete normally or abruptly. If the JVM terminates because a thread invoked `Runtime.halt` _while shutdown hooks were running_, then, in addition to daemon and non-daemon threads, any shutdown hook that has not yet terminated will execute no further Java code. Native applications can use the JNI Invocation API to create and destroy the Java Virtual Machine in such a way that a Java program, having started execution in the `main` method of an initial class (JLS 12.1), subsequently exits when all of its non-daemon threads have terminated (JLS 12.8). The Java Virtual Machine does not terminate "automatically" when the last non-daemon thread terminates.
09-08-2022