Java Shutdown Hook
Shutdown hook in Java.
Generally, finally block is used to do some required activities before returning the control to calling class.
If try block is executed successfully, then finally block is executed. If try block has System.exit(), then it doesn't
give chance to finally block to execute as jvm shuts down, and the activities in finally block are not executed.
So there are other following reasons when finally block is not executed due to JVM shuts down-
◦user presses ctrl+c on the command prompt
◦user logoff
◦user shutdown etc.
So, there can be certain activities which you would like to execute before such events happen, like some clean-up activities.
So in these scenarios we can't rely on finally block to execute those activities. To handle such events we can use
shutdown hooks provided in Java. First create your shutdown hooks to implement the activities which you want to execute,
then register them with Runtime.
So if jvm gets shut down, then your code in shutdown hook is executed before application exits.
One sample is given below with some explanation in it.
But if halt() from runtime is executed then it doesn't give chance to shutdown hooks to execute even, so use it with care
as explained in Java doc.
public class ShutdownHooks {
public static void main(String[] args) {
Runtime r = Runtime.getRuntime();
try {
ShutDown sd = new ShutDown("Hello");
r.addShutdownHook(new Thread(sd));
System.out.println("Before exit.....");
/**
* Though finally block is always executed on the completion of try block
* but when use below method then we are not allowing try block to complete
* and it is getting terminated abruptly in the middle, so finally block is
* not executed. But it doesn't stop registered shutdown hooks to execute,
* so registered shutdown hook is executed here.
*/
System.exit(1);
System.out.println("After exit......");
} finally {
System.out.println("In finally!!");
/**
* As per Java doc -
* Forcibly terminates the currently running Java virtual machine. This method never returns normally.
* This method should be used with extreme caution. Unlike the exit method,
* this method does not cause shutdown hooks to be started and does not run uninvoked finalizers
* if finalization-on-exit has been enabled. If the shutdown sequence has already been initiated
* then this method does not wait for any running shutdown hooks or finalizers to finish their work.
*/
r.halt(1);
}
}
/**
* Generally finalize method is used for some cleanup activities related to that object before it is garbage collected.
* But it is not guaranteed when, so one should not rely on this method to release the connections or resources.
* It is also not executed when jvm shuts down.
*/
@Override
protected void finalize() {
System.out.println("Inside finalize().....");
}
}
class ShutDown implements Runnable {
String name;
public ShutDown(String name) {
this.name = name;
System.out.println("Will Clean " + name);
}
/**
* Shutdown hook is executed in separate thread, so it will need to be passed all the required information for the
* activities being executed here.
*/
@Override
public void run() {
System.out.println("In shutdown hook...........");
System.out.println("Cleaned " + name);
}
}
Generally, finally block is used to do some required activities before returning the control to calling class.
If try block is executed successfully, then finally block is executed. If try block has System.exit(), then it doesn't
give chance to finally block to execute as jvm shuts down, and the activities in finally block are not executed.
So there are other following reasons when finally block is not executed due to JVM shuts down-
◦user presses ctrl+c on the command prompt
◦user logoff
◦user shutdown etc.
So, there can be certain activities which you would like to execute before such events happen, like some clean-up activities.
So in these scenarios we can't rely on finally block to execute those activities. To handle such events we can use
shutdown hooks provided in Java. First create your shutdown hooks to implement the activities which you want to execute,
then register them with Runtime.
So if jvm gets shut down, then your code in shutdown hook is executed before application exits.
One sample is given below with some explanation in it.
But if halt() from runtime is executed then it doesn't give chance to shutdown hooks to execute even, so use it with care
as explained in Java doc.
public class ShutdownHooks {
public static void main(String[] args) {
Runtime r = Runtime.getRuntime();
try {
ShutDown sd = new ShutDown("Hello");
r.addShutdownHook(new Thread(sd));
System.out.println("Before exit.....");
/**
* Though finally block is always executed on the completion of try block
* but when use below method then we are not allowing try block to complete
* and it is getting terminated abruptly in the middle, so finally block is
* not executed. But it doesn't stop registered shutdown hooks to execute,
* so registered shutdown hook is executed here.
*/
System.exit(1);
System.out.println("After exit......");
} finally {
System.out.println("In finally!!");
/**
* As per Java doc -
* Forcibly terminates the currently running Java virtual machine. This method never returns normally.
* This method should be used with extreme caution. Unlike the exit method,
* this method does not cause shutdown hooks to be started and does not run uninvoked finalizers
* if finalization-on-exit has been enabled. If the shutdown sequence has already been initiated
* then this method does not wait for any running shutdown hooks or finalizers to finish their work.
*/
r.halt(1);
}
}
/**
* Generally finalize method is used for some cleanup activities related to that object before it is garbage collected.
* But it is not guaranteed when, so one should not rely on this method to release the connections or resources.
* It is also not executed when jvm shuts down.
*/
@Override
protected void finalize() {
System.out.println("Inside finalize().....");
}
}
class ShutDown implements Runnable {
String name;
public ShutDown(String name) {
this.name = name;
System.out.println("Will Clean " + name);
}
/**
* Shutdown hook is executed in separate thread, so it will need to be passed all the required information for the
* activities being executed here.
*/
@Override
public void run() {
System.out.println("In shutdown hook...........");
System.out.println("Cleaned " + name);
}
}