Java Articles

Advertisement

Java ShutdownHook Example

by Ram Satish

Share

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 {
@Override
public void run() {
...
}
}
Advertisement

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

MyServer:

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");
instance.start(args);
} catch (final Exception ex) {
instance.stop(-1);
}
System.exit(instance.resultCode);
}

private void start(String[] args) {
if (shutdownHook == null) {
shutdownHook = new ShutdownHook();
}
Runtime.getRuntime().addShutdownHook(shutdownHook);
await();
}

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");
t.interrupt();
try {
System.out.println("Wait till main thread dies");
t.join(1000);
} catch (InterruptedException e) {
}
}
}

private class ShutdownHook extends Thread {
@Override
public void run() {
if (instance.serverRunning) {
System.out.println("Stop the server");
instance.stop(0);
} else {
System.out.println("Shutdown already called.");
}
}
}
}
Advertisement

Let’s test ShutdownHook

Run the program in console.
Press Control-C

Output:

C:\javarticles_ws\javaShutdownHookExample\bin>java com.javarticles.concurrency.M
yServer
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: javaShutdownHookExample.zip

Advertisement

Share

Related

Advertisement

Latest

Advertisement