Thursday, June 19, 2008

Shutdown Hooks in Java? How are they used?


Shutdown Hooks in Java

public void addShutdownHook(Thread hook) - this method of the class java.lang.Runtime is used to register a new shutdown hook for the underlying JVM. A Shut Down Hook is an initialized thread which has not been started so far. When the JVM starts shutting down then it starts all the registered shutdown hooks in some unspecified order and all the shutdown hooks start running concurrently.

Are they guaranteed to complete execution?

It's not guaranteed that all (or even a single) the shutdown hooks registered with the JVM will complete their execution during the JVM shut down process. In some (though very rare) circumstances JVM may simply abort and may shut down abruptly. Such a situation may arise when the underlying operating system forces the JVM to stop immediately. For example, the SIGKILL signal on an Unix machine or the TerminateProcess call on a Windows machine can lead to such a situation. There may few other reasons for the JVM to abort itself forcibly - it does so if it identifies that a native method is not running as expected.

How to stop a running Hook? What should not they be used for?

Most of the operating systems require the JVM to shut down within a specified time frame. Therefore the JVM may call Runtime.halt() method to abruptly terminate all the pending jobs including abort of all the non-terminated shutdown hooks. Once a shutdown hook starts its execution, only Runtime.halt() can forcibly terminate it. Even in the above mentioned rare scenarios, the JVM uses Runtime.halt() method only to abort the still-executing shutdown hooks. Since, an OS may have a maximum allowed time limit for JVM shut down, hence the shutdown hooks should not be used for long running tasks or for those tasks which involve user interaction.

What happens if a Shutdown Hook throws an Uncaught Exception?

No special treatment for Shutdown Hooks in this regard. They are treated like normal threads only. An uncaught exception invokes uncaughtException method of the thread's ThreadGroup object, which by default writes the stack trace to System.err and then simply terminates the thread.

What happens if you try to register/de-register hooks during shutdown?

An attempt to register or de-register shutdown hooks while the JVM shutdown process is already in progress will cause an IllegalStateException to be thrown.

Caution to be taken while writing Shutdown Hooks

Since they are expected to complete their execution very quickly, so we should avoid putting code which may require (even in some cases) a longer time to complete. These hooks should be thread-safe and they should not cause any deadlock to happen. It's better not to use those services in a shutdown hook which have got their own shutdown hooks registered because they themselves may be in the process of shutting down.


Update[June 23, 2008]: Read more on Shutdown Hooks - Common Questions on Shutdown Hooks in Java >>

Liked the article? You may like to Subscribe to this blog for regular updates. You may also like to follow the blog to manage the bookmark easily and to tell the world that you enjoy GeekExplains. You can find the 'Followers' widget in the rightmost sidebar.



Share/Save/Bookmark


4 comments:

Chris P. said...

Good article, never really gave them much thought before but I guess I should now !

මගේ සයිබර් නිවහන said...

yes..it's a good article..........

මගේ සයිබර් නිවහන said...

yes.it's a good article

Unknown said...

Short and concise...apt for beginners.