Java Shutdown hook - Runtime.addShutdownHook() With Examples

Java shutdown hook are handy to run some code when program exit. We can use java.lang.Runtime.addShutdownHook(Thread t) method to add a shutdown hook in the JVM.

Java shutdown hook

Java shutdown hook runs in these two cases.

  1. The program exits normally, or we call System.exit() method to terminate the program. Read more about Java System Class.
  2. User interrupts such as Ctrl+C, system shutdown etc.

Important points about Java Shutdown Hook are;

  1. We can add multiple shutdown hooks using Runtime addShutdownHook() method.
  2. Shutdown hooks are initialized but not-started threads. They start when JVM shutdown triggers.
  3. We can’t determine the order in which shutdown hooks will execute, just like multiple threads executions.
  4. All un-invoked finalizers are executed if finalization-on-exit has been enabled.
  5. There is no guarantee that shutdown hooks will execute, such as system crash, kill command etc. So you should use it only for critical scenarios such as making sure critical resources are released etc.
  6. You can remove a hook using Runtime.getRuntime().removeShutdownHook(hook) method.
  7. Once shutdown hooks are started, it’s not possible to remove them. You will get IllegalStateException.
  8. You will get SecurityException if security manager is present and it denies RuntimePermission("shutdownHooks").

Java shutdown hook example

So let’s see example of shutdown hook in java. Here is a simple program where I am reading a file line by line from some directory and processing it. I am having program state saved in a static variable so that shutdown hook can access it.

package com.journaldev.shutdownhook;
public class FilesProcessor {
	public static String status = "STOPPED";
	public static String fileName = "";
	public static void main(String[] args) {
		String directory = "/Users/pankaj/temp";
		Runtime.getRuntime().addShutdownHook(new ProcessorHook());
		File dir = new File(directory);
		File[] txtFiles = dir.listFiles(new FilenameFilter() {
			public boolean accept(File dir, String name) {
				if (name.endsWith(".txt"))
					return true;
					return false;
		for (File file : txtFiles) {
			BufferedReader reader = null;
			status = "STARTED";
			fileName = file.getName();
			try {
				FileReader fr = new FileReader(file);
				reader = new BufferedReader(fr);
				String line;
				line = reader.readLine();
				while (line != null) {
					Thread.sleep(1000); // assuming it takes 1 second to process each record
					// read next line
					line = reader.readLine();
				status = "PROCESSED";
			} catch (IOException | InterruptedException e) {
				status = "ERROR";
				try {
				} catch (IOException e) {

Most important part of above code is line no 16 where we are adding the shutdown hook, below is the implementation class.

package com.journaldev.shutdownhook;
public class ProcessorHook extends Thread {
	public void run(){
			System.out.println("Seems some error, sending alert");

It’s a very simple use, I am just logging state when the shutdown hook started and if it was not finished already, then send an alert (Not actually, its just logging here).

Let’s see some program execution through terminal.

  1. Program terminated using Ctrl+C command.
  2. java-shutdown-hook-example
  3. As you can see in the above image, shutdown hook started executing as soon as we tried to kill the JVM using Ctrl+C command.
  4. Program executed normally.
  5. java-shutdown-hook-example
  6. Notice that hook is called in case of normal exit too, and it’s printing status as finished.
  7. Kill Command to terminate JVM.
  8. java-shutdown-hook-example
  9. I used two terminal windows to fire kill command to the java program, so operating system terminated the JVM and in this case shutdown hook was not executed.

That’s all for shutdown hook in java, I hope you learned about JVM shutdown hooks and might find a use for it.

Reference: API Doc

By admin

Leave a Reply

%d bloggers like this: