SrcForge

Lesson 49

Shutdown Hook

A shutdown hook is a thread that runs automatically when the JVM begins shutting down.

Java Track

Save progress as you learn.

61 lessons

Lesson content

A shutdown hook is a thread registered with the JVM that executes cleanup tasks before the application terminates.

Definition List

  • Shutdown Hook: A thread that executes when the JVM begins shutting down.
  • JVM: The runtime environment that executes Java applications.
  • Cleanup Task: An operation performed before termination, such as closing files.
  • Resource Management: The process of releasing system resources properly.
  • Runtime Class: A Java class providing access to the current JVM environment.

Shutdown Process Flow

Application Running

Shutdown Requested

Shutdown Hooks Execute

Cleanup Tasks Complete

JVM Terminates

Registering a Shutdown Hook

// Register a shutdown hook
Runtime.getRuntime().addShutdownHook(
    new Thread(() -> {
        System.out.println("Cleanup completed");
    })
);

// Remove a previously registered hook
Runtime.getRuntime().removeShutdownHook(thread);

Example: Java Shutdown Hook

package com.java.Multi_threading;

public class ShutdownHookExample {

    public static void main(String[] args) {

        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            System.out.println("Shutdown hook executed");
        }));

        System.out.println("Program is running");

        for (int i = 0; i < 5; i++) {
            System.out.println("Working... " + i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.exit(0);
    }
}

Output

Program is running
Working... 0
Working... 1
Working... 2
Working... 3
Working... 4
Shutdown hook executed

When Hooks Execute vs When They Don't

  • Executes — main() method completes normally.
  • Executes — System.exit(0) is called.
  • Executes — user sends Ctrl+C interrupt.
  • Does NOT execute — Runtime.getRuntime().halt(0) is called.
  • Does NOT execute — OS crash or hardware failure occurs.

Best Practices

  • Keep hooks lightweight; perform only essential cleanup.
  • Close files, database connections and network resources.
  • Handle exceptions inside the hook to prevent silent failures.
  • Avoid long-running operations inside hooks.
  • Test shutdown behavior across different termination scenarios.

FAQ

1. What is a shutdown hook in Java?

A thread registered with the JVM that executes cleanup tasks before the application terminates.

2. How do you register a shutdown hook?

Use Runtime.getRuntime().addShutdownHook() and pass a new Thread with cleanup logic.

3. Are shutdown hooks always executed?

No. Forced termination via halt(), system crashes, or hardware failures can bypass them.

Shutdown hooks run automatically before JVM exits.
Register using Runtime.getRuntime().addShutdownHook().
Runs on System.exit(), Ctrl+C and normal termination.
Does NOT run on Runtime.halt() or system crashes.
Multiple hooks may execute concurrently — no guaranteed order.
Keep hooks fast; long tasks delay JVM shutdown.
Use for closing files, DB connections and background services.

Where Java is used

Java in real-world development

Shutdown Hook

A thread that runs automatically when the JVM starts its shutdown sequence.

addShutdownHook()

Registers a thread to execute cleanup logic before JVM exit.

removeShutdownHook()

Removes a previously registered shutdown hook thread.

Runtime.halt()

Forces JVM to stop immediately — shutdown hooks do NOT run.

System.exit()

Initiates normal JVM shutdown; registered hooks will execute.

Shutdown Hook vs Daemon Thread

Hook runs during shutdown for cleanup; daemon runs while JVM is active.

Prev: ThreadGroupBack to hub