Monday, June 23, 2008

Common Questions on Shutdown Hooks in Java

Common Questions on Shutdown Hooks in Java

If you like to refresh your understanding of Shutdown Hooks then read this article first - Shutdown Hooks in Java >>

Question: Why doesn't Java provide infomation as to why the JVM is shutting down?
Answer: Because it can not be done in a portable way. There are certain platforms which have capabilities of notifying the potential causes of a JVM shutdown such as power failure, shut down or restart of the OS, etc., but there are some platforms where such information is not available and hence this functionality can't be implemented in a platform independent way.

Question: Will Shutdown hooks run if the JVM crashes?
Answer: No guarantee if the crash is due to an error occured while executing some native system code.

Question: Can't we do the same with the method runFinalizersOnExit?
Answer: No... runFinalizersOnExit will only be executed if the JVM shuts down due to exit and not for those termination-triggered shutdowns of the Java Virtual Machine. BTW, runFinalizersOnExit is already a deprecated method due as it's claimed to be inherently unsafe. Sun Javadoc says that this method may result in finalizers being called on live objects while other threads are concurrently manipulating them and hence this method may cause erratic behavior or a possible deadlock.

Question: Can you force an ordered execution of Shutdown Hooks in Java?
Answer: Yes, but only indirectly... as we can have the same effect by doing some manipulation (synchronizing them), but we can certainly not force an order of execution of the registered Shutdown Hooks explicitly. The Shutdown Hooks in Java are by default executed in some unspecified order, but we can synchronize the actions internally to force a particular order of execution of all those shutdown actions.

Question: Can an untrusted applet register a Shutdown Hook?
Answer: No... provided a Security Manager is installed. Both the methods - addShutdownHook and removeShutdownHook first check the permissions granted to the caller of these methods and these methods are executed only if the caller's security context permits RuntimePermission("shutdownHooks") otherwise the call raises a SecurityException. In case of an untrusted applet, we'll get this exception only as 'untrusted' means that applet doesn't have permissions.

Question: What is done if the Shutdown Hooks cause a deadlock OR if they start taking too much time to complete?
Answer: Runtime.halt() method is used in such cases to abort all the pending/running Shutdown Hooks and the JVM terminates immediately.

Question: Why the finalizers are run after all the registered Shutdown Hooks?
Answer: Because a Shutdown Hook may need to access some live objects which might have been finalized pre-maturely by the finalizers in case they run before the registered Shutdown Hooks. Remember that runFinalizersOnExit is an inherently unsafe method and enabling finalizers may result in calling finalizers on live objects as well.

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.


No comments: