---
title: "Shutdown Hook in Java"  
description: "Before JVM shuts down if you want to execute some code, use shutdown hook."  
author: "Anupam Mishra"  
published: 2015-12-10  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/11940/shutdown-hook-in-java  
category: "java"  
tags: ["java", "thread", "threading", "thread abort", "j2ee"]  
reading_time: 2 minutes  

---

# Shutdown Hook in Java

The [shutdown hook](https://www.mindstick.com/interview/2496/what-is-shutdown-hook) is used to perform cleanup resources or saving state i.e. sending some alerts ,closing [log files](https://www.mindstick.com/forum/23305/can-t-shrink-my-sql-server-2008-log-files-260gb-currently) ,close the [connections](https://yourviews.mindstick.com/view/87707/baba-siddique-murder-news-his-connections-with-underworld) or [something else](https://answers.mindstick.com/qa/114812/what-defines-a-ghost-is-it-a-spirit-energy-imprint-or-something-else-entirely) before JVM goes shutdown as normally or abortly. If you want to execute some code before JVM shuts down, use shutdown hook.

##### The following cases JVM shuts down when:

\

- User presses Alt+F4 or ctrl+c on the [command prompt](https://answers.mindstick.com/qa/51716/how-to-make-bootable-pendrive-using-cmd-windows-command-prompt).
- System.exit(0) method is invoked
- User logoff
- User shutdown etc.

##### So we have firstly override the method

[public void](https://www.mindstick.com/interview/2715/can-we-write-static-public-void-instead-of-public-static-void) addShutdownHook(Runnable r){} of my java program.

The addShutdownHook() method of [Runtime class](https://www.mindstick.com/interview/2457/what-is-the-purpose-of-the-runtime-class) is used to registering the

thread with the [Virtual Machine](https://www.mindstick.com/forum/157900/what-is-a-virtual-machine-and-how-does-it-work) i.e. JVM.

The object of Runtime class can be obtained by calling the static [factory method](https://www.mindstick.com/forum/34032/factory-method-design-pattern-using-c-sharp) getRuntime(). Runtime r = Runtime.getRuntime();

The method that returns the instance of a class is [known as](https://answers.mindstick.com/qa/38482/name-the-world-s-lightest-material-which-was-manufactured-by-isro-scientists-at-the-vikram-sarabhai-space-centre-thiruvananthapuram-and-is-also-known-as-blue-air) factory method.

The shutdown sequence can be stopped by invoking the halt (int) method of Runtime class.

For example, we have created MyThread class and override run method .When we invoked to another class that is using Shutdown Hook to given MyThread class object.

```
class MyThread extends Thread{
    public void run(){
System.out.println("shut down hook task completed..");
    }
}
public class ShutdownEx{
public static void main(String[] args)throws Exception
{
Runtime r=Runtime.getRuntime();
r.addShutdownHook(new MyThread());  //add thread to Shutdown Hook
System.out.println("Now I am sleeping... press ctrl+c to exit");
//r.halt(5000);
try{Thread.sleep(5000);}catch (Exception e) {}
}
} 
```

/// In output JVM waits 5 second of the finishing this program

> ![Shutdown Hook in Java](https://www.mindstick.com/mindstickarticle/8d0daf64-acd6-420c-8f53-5391560d86d2/images/cbf0a3d2-c7af-4d4b-af79-d0ee24c0c4d9.png)\

---

Original Source: https://www.mindstick.com/articles/11940/shutdown-hook-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
