Module java.base
Package java.lang

Class Runtime

java.lang.Object
java.lang.Runtime

public classRuntimeextendsObject
Every Java application has a single instance of classRuntime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from thegetRuntime method.

An application cannot create its own instance of this class.

Shutdown Sequence

The Java Virtual Machine initiates theshutdown sequence in response to one of several events:

  1. when the number oflive non-daemon threads drops to zero for the first time (see note below on the JNI Invocation API);
  2. when theRuntime.exit orSystem.exit method is called for the first time; or
  3. when some external event occurs, such as an interrupt or a signal is received from the operating system.

At the beginning of the shutdown sequence, the registered shutdown hooks arestarted in some unspecified order. They run concurrently with any daemon or non-daemon threads that werealive at the beginning of the shutdown sequence.

After the shutdown sequence has begun, registration and de-registration of shutdown hooks withaddShutdownHook andremoveShutdownHook is prohibited. However, creating and starting new threads is permitted. New threads run concurrently with the registered shutdown hooks and with any daemon or non-daemon threads that are already running.

The shutdown sequence finishes when all shutdown hooks have terminated. At this point, the Java Virtual Machine terminates as described below.

It is possible that one or more shutdown hooks do not terminate, for example, because of an infinite loop. In this case, the shutdown sequence will never finish. Other threads and shutdown hooks continue to run and can terminate the JVM via thehalt method.

Prior to the beginning of the shutdown sequence, it is possible for a program to start a shutdown hook by calling itsstart method explicitly. If this occurs, the behavior of the shutdown sequence is unspecified.

Java Virtual Machine Termination

The JVM terminates when the shutdown sequence finishes or whenhalt is called. In contrast toexit, thehalt method does not initiate the shutdown sequence.

When the JVM terminates, all threads are immediately prevented from executing any further Java code. This includes shutdown hooks as well as daemon and non-daemon threads. This means, for example, that:

  • threads' current methods do not complete normally or abruptly;
  • finally clauses are not executed;
  • uncaught exception handlers are not run; and
  • resources opened with try-with-resources are notclosed;

Implementation Note:
Native code typically uses theJNI Invocation API to control launching and termination of the JVM. Such native code invokes theJNI_CreateJavaVM function to launch the JVM. Subsequently, the native code invokes theDestroyJavaVM function to await termination of that JVM. TheDestroyJavaVM function is responsible for initiating the shutdown sequence when the number oflive non-daemon threads first drops to zero. When the shutdown sequence completes and the JVM terminates, control is returned to the native code that invokedDestroyJavaVM. This behavior differs from theexit orhalt methods. These methods typically terminate the OS process hosting the JVM and do not interact with the JNI Invocation API.
SeeJava Language Specification:
12.8 Program Exit
Since:
1.0
External Specifications
See Also: