Skip to content

Shutdown hooks

Shutdown hooks are threads that are registered with the Java Runtime instance to execute specific tasks during the JVM shutdown process.^[600-developer-java-java-base-java-runtime-number.md] This mechanism allows developers to perform cleanup operations or resource release tasks gracefully before the application terminates.

Registration and Management

Shutdown hooks are initialized and registered using the addShutdownHook method of the Runtime class.^[600-developer-java-java-base-java-runtime-number.md] Multiple hooks can be registered within a single application lifecycle.^[600-developer-java-java-base-java-runtime-number.md]

If necessary, a previously registered hook can be removed using the removeShutdownHook method, provided the JVM has not already started its shutdown sequence.^[600-developer-java-java-base-java-runtime-number.md]

Termination Behavior

The execution of shutdown hooks can be interrupted or bypassed if the JVM is forcibly terminated. For example, calling Runtime.halt() will stop the virtual machine immediately, preventing any registered shutdown hooks from running.^[600-developer-java-java-base-java-runtime-number.md]

Sources

^[600-developer-java-java-base-java-runtime-number.md]