JVM Shutdown Hooks

Shutdown hooks let Java run cleanup logic when the JVM begins an orderly shutdown. They are useful for releasing resources, flushing state, and performing application-level cleanup before the process exits.

A shutdown hook is a thread registered with the JVM through `Runtime.addShutdownHook`. When the JVM starts an orderly shutdown, it begins the registered hooks concurrently. The application can use these hooks for cleanup that should happen during normal JVM termination.

A hook is registered with `Runtime.getRuntime().addShutdownHook(thread)`. The thread is not started when it is registered; the JVM starts it as part of shutdown. Because multiple hooks may run at the same time, shared state accessed by hooks should be designed for concurrency.

Shutdown hooks can run when the JVM terminates because of normal program completion, `System.exit(...)`, or another orderly shutdown condition. They should be kept short and should avoid depending on services that may already be unavailable during shutdown.

Shutdown hooks are not a guarantee that cleanup will run for every possible process termination. For example, forcibly terminating the JVM or operating-system process can prevent hooks from executing. Hooks also should not be treated as a substitute for deterministic resource management such as `try`-with-resources.

Key points

Interview questions

What is a JVM shutdown hook, and how do you register one?

A shutdown hook is a thread that the JVM runs when it begins an orderly shutdown. It is registered with `Runtime.getRuntime().addShutdownHook(...)`. The thread is registered but not started immediately. During shutdown, the JVM starts the hook so it can perform cleanup work.

When does a shutdown hook run?

A shutdown hook runs when the JVM enters its shutdown sequence. This includes normal termination and calls such as `System.exit(...)`. Registered hooks are started during shutdown rather than during registration. Abrupt termination that bypasses normal shutdown can prevent them from running.

Do multiple shutdown hooks run in a guaranteed order?

No. The JVM starts registered shutdown hooks concurrently, so application code should not rely on registration order. If hooks share mutable state, they need appropriate synchronization or another concurrency-safe design. A dependency between cleanup operations should generally be coordinated explicitly rather than assumed from hook order.

Should shutdown hooks be used for all resource cleanup in a Java application?

No. Resources with well-defined ownership should normally be cleaned up deterministically, such as with `try`-with-resources. A shutdown hook is better suited to process-level cleanup that is specifically needed when the JVM terminates. It is also not guaranteed to run during every possible termination scenario.

What should you consider when designing a shutdown hook for a backend service?

The hook should be bounded, concurrency-safe, and independent of components that may already be shutting down. It should avoid blocking indefinitely and should handle cleanup failures deliberately. For a backend service, shutdown hooks can complement an application's graceful shutdown mechanism, but they should not replace coordinated service shutdown, request draining, or deterministic resource management.