Core Java

JVM Shutdown Hook in Java

This article is a tutorial on implementing a simple JVM Shutdown Hook in Java. In this example, we will take a look at different ways we can terminate a JVM application.

1. Introduction

Shutdown Hooks allow developers to plug in the desired code to be executed when the JVM is shutting down. This is useful when we need to do special clean-up operations before the VM finishes terminating. Shutdown hooks are basically initialized but unstarted threads. When the JVM begins its shutdown process, it will start all registered hooks in an unspecified order. After running all hooks, the JVM will halt.

Also, Shutdown Hooks are also executed even when the VM is shutting down due to an external reason like a kill request from the O/S. The general methods such as System.exit(0) will only work if the VM is terminated by a controlled process.

2. Technologies used

  • Java SE 14

3. Adding Hooks

In order to add a shutdown hook, we can use the Runtime.getRuntime().addShutdownHook() method:

Adding Hook

Runtime.getRuntime().addShutdownHook(new Thread(){  
public void run(){  
    System.out.println("Shutdown Hook running");  
    }  
});

The way it works is that a created thread with the desired code to be executed is defined, and then we initialize it by adding it to the current runtime.

4. JVM Shutdown Hook Simple Example

Simple Example

class MyShutdownHookThread extends Thread {
	public void run() {
		System.out.println("Main terminated. We are in Shutdown Hook.\nBye!");
	}
}

public class ShutdownHook {
	public static void main(String[] args) throws Exception {

		Thread shutdownHook = new MyShutdownHookThread();
		Runtime.getRuntime().addShutdownHook(shutdownHook);

		System.out.println("Now main is running.");

	}
}
  • line 1: we create a new thread class that will actually be out Shutdown Hook.
  • line 2: inside the method run(), we write the code we want to execute
  • line 11: inside our main code, we initialize our hook bypassing the thread class we created as a variable in the addShutdownHook() method
JVM Shutdown Hook - Output of ShutdownHook class.
Fig. 1. Output of ShutdownHook class.

5. Removing a Shutdown Hook

Java also gives as the ability to remove a shutdown hook if it is already defined by using the method

Simple Example with Removing Hook

class RemovedShutdownHookThread extends Thread {
	public void run() {
		System.out.println("Main terminated. We are in Shutdown Hook.\nBye!");
	}
}

public class RemoveShutdownHook {
	public static void main(String[] args) throws Exception {

		Thread shutdownHook = new RemovedShutdownHookThread();
		Runtime.getRuntime().addShutdownHook(shutdownHook);

		System.out.println("Now main is running.");

		Runtime.getRuntime().removeShutdownHook(shutdownHook);
	}
}
  • line 15: we are removing the hook bypassing the name in removeShutdownHook() method
JVM Shutdown Hook - Output of ShutdownHook class with remove method.
Fig. 2. Output of ShutdownHook class with remove method

6. Summary

In these examples, we demonstrated how we can use the Shutdown Hook methods that Java provides us. As we noticed, even though we initialized the Shutdown Hook before the main code of our program, it ran after the program terminated. This is very helpful as it gives us flexibility and a safe way to run a specific part of code e.g memory clean up.

7. Download the source code

This was an example of how to create and remove a JVM Shutdown Hook in Java.

Download
You can download the full source code of this example here: JVM Shutdown Hook in Java

Odysseas Mourtzoukos

Mourtzoukos Odysseas is studying to become a software engineer, at Harokopio University of Athens. Along with his studies, he is getting involved with different projects on gaming development and web applications. He is looking forward to sharing his knowledge and experience with the world.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button