Google News
logo
Java Multithreading - Interview Questions
What is shutdown hook?
The shutdown hook is a thread that is invoked implicitly before JVM shuts down. So we can use it to perform clean up the resource or save the state when JVM shuts down normally or abruptly. We can add shutdown hook by using the following method :
public void addShutdownHook(Thread hook){}    
Runtime r=Runtime.getRuntime();  
r.addShutdownHook(new MyThread());  
Some important points about shutdown hooks are :
 
* Shutdown hooks initialized but can only be started when JVM shutdown occurred.
* Shutdown hooks are more reliable than the finalizer() because there are very fewer chances that shutdown hooks not run.
* The shutdown hook can be stopped by calling the halt(int) method of Runtime class.
Advertisement