Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do I need to call this method Runtime.getRuntime().addShutdownHook()

Tags:

java

When do I actually need call this method Runtime.getRuntime().addShutdownHook() and when or why I need to shutdown my application. Could anyone please explain me this by giving some example.

Thanks

like image 920
Harry Avatar asked Jan 04 '12 06:01

Harry


People also ask

What is runtime getRuntime () addShutdownHook?

getRuntime(). addShutdownHook(new Thread() { public void run() { System. out. println("Running Shutdown Hook"); } }); will print a Running Shutdown Hook at the time of program termination at any point.

What is ADD shutdown hook in Java?

The java.lang.Runtime.addShutdownHook(Thread hook) method registers a new virtual-machine shutdown hook.The Java virtual machine shuts down in response to two kinds of events − The program exits normally, when the last non-daemon thread exits or when the exit (equivalently, System.exit) method is invoked, or.

When shutdown hook is called?

A shutdown hook is simply an initialized but unstarted thread. When the virtual machine begins its shutdown sequence it will start all registered shutdown hooks in some unspecified order and let them run concurrently.


2 Answers

As far as I know, I will explain this below. You can google it and find lot of information too.

addShutdownHook() will register some actions which is to be performed on a Program's termination. The program that you start ends in two ways:

  1. the main thread (Root) ends its running context;
  2. the program meets some unexpected situation, so it cannot proceed further.

If you add a ShutdownHook, the hook will start a thread that will start running at time of termination only. For example:

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

will print a Running Shutdown Hook at the time of program termination at any point. You might even call a System.exit(0).

For examples, you can google, there are enough of them. And the question 'When should you use this' is like asking 'What does catch do in a try-catch statement'.

You might have many situations like:

  • your program had created many temporary files in filesystem you want to delete it;
  • you need to send a distress signal to another process/machine before terminating;
  • execute any clean-up actions, logging or after-error actions on unexpected behaviours.

All this will be needed for some point of time.

For examples you can go in here Example 1 or Example 2

like image 177
Kris Avatar answered Oct 09 '22 05:10

Kris


You only worry about shutdown hooks when you want something to happen when a shutdown occurs on the virtual machine.

From Javadoc:

The Java virtual machine shuts down in response to two kinds of events:

  • The program exits normally, when the last non-daemon thread exits or when the exit (equivalently, System.exit) method is invoked, or
  • The virtual machine is terminated in response to a user interrupt, such as typing ^C, or a system-wide event, such as user logoff or system shutdown.

Thus, a shutdown hook is a initialized and unstarted thread that gets executed when a JVM shutdown occurs.

Popular examples of shutdown hooks exists in application servers (such as JBoss AS). When you press Ctrl+C, the JVM calls all the Runtime shutdown hooks registered (such as JBoss shutdown hooks) before exiting.

like image 24
Buhake Sindi Avatar answered Oct 09 '22 06:10

Buhake Sindi