Java ShutdownHook Example


A shutdown hook helps an application to shutdown gracefully.

You can register multiple shutdown hooks. When the virtual machine begins its shutdown, its going to start calling each of the registered shutdown hooks.

An llegalArgumentException is thrown if the hook has already been registered. <code?IllegalStateException is thrown if the virtual machine is already in the process
of shutting down.

A shutdown hook is an unstarted instance of a Thread object which you will add it to the application calling Runtime.getRuntime().addShutdownHook(shutdownHook).
Here is a simple example:

    //Create shutdown hook and register
    private void start(String[] args) {
        Runtime.getRuntime().addShutdownHook(new ShutdownHook());

    private class ShutdownHook extends Thread {
        public void run() {            

Detailed Shutdown Hook Example

  1. Create server instance
  2. Set boolean member serverRunning to true
  3. Start the instance
  4. In case of any exception, catch it and stop the instance. Exit the JVM with an abnormal status
  5. Register the shutdown hook right at the beginning of the start of server instance
  6. Let the main thread await infinitely in a loop
  7. In the shutdown hook, verify on serverRunning boolean if the program is still running. If yes, stop the server instance
  8. If serverRunning returns false then it means server shutdown is already in progress.

What should you do in server shutdown?

  1. Set serverRunning to false
  2. Interrupt the working threads if they wait for data in some blocking call)
  3. Interrupt the main thread
  4. Wait till the main thread dies


package com.javarticles.concurrency;

public class MyServer {
    private static MyServer instance;
    private static Thread mainThread;
    private boolean serverRunning;
    private int resultCode;
    private Thread shutdownHook = null;
    public MyServer() {
        resultCode = 0;
        serverRunning = true;
    public static void main(String[] args) {
        try {
            instance = new MyServer();
            System.out.println("Start server");
        } catch (final Exception ex) {
    private void start(String[] args) {
        if (shutdownHook == null) {
            shutdownHook = new ShutdownHook();
    public void await() {
        try {
            mainThread = Thread.currentThread();
            while(serverRunning) {
                try {
                    Thread.sleep( 10000 );
                } catch( InterruptedException ex ) {
        } finally {
            mainThread = null;
    private void stop(int i) {
        System.out.println("Stopping instance");        
        serverRunning = false;
        Thread t = mainThread;
        if (t != null) {          
            System.out.println("Interrupt main thread");
            try {
                System.out.println("Wait till main thread dies");
            } catch (InterruptedException e) {
    private class ShutdownHook extends Thread {
        public void run() {            
            if (instance.serverRunning) {
                System.out.println("Stop the server");
            } else {
                System.out.println("Shutdown already called.");

Let’s test ShutdownHook

Run the program in console.
Press Control-C


C:\javarticles_ws\javaShutdownHookExample\bin>java com.javarticles.concurrency.M
Start server
Stop the server
Stopping instance
Interrupt main thread
Wait till main thread dies

Download the source code

This was an example about Java ShutdownHook.

You can download the source code here:

Comments are closed.