Friday 23 September 2016

Runtime.addShutdownHook(): Run code at JVM exit

By using addShutdownHook method, we can attach a piece of code to JVM. This piece of code executes when the Java Virtual Machine starts its shutdown sequence.

When the JVM Shutdown?
JVM shutdown in following two scenarios.

Scenario 1
The program exits normally, when the last non-daemon thread exits or when the System.exit method is invoked,

Scenario 2
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.

Syntax
public void addShutdownHook(Thread hook)

Example
Runtime.getRuntime().addShutdownHook(new Thread() {

 @Override
 public void run() {
  System.out.println("Application exited successfully without any unhandled error. ");
 }
});

public class ShutDownHookEx {
 public static void main(String args[]) {
  Runtime.getRuntime().addShutdownHook(new Thread() {
   @Override
   public void run() {
    System.out.println("Application exited successfully without any unhandled error. ");
   }
  });

  System.out.println("About to Exit the Application");
  System.exit(0);
  System.out.println("Application Exited");
 }

}

Run above application, you can able to see following messages in the console.

About to Exit the Application
Application exited successfully without any unhandled error.

Can I add multiple shutdown hooks to JVM?
Yes, you can add multiple shutdown hooks to JVM. When the virtual machine begins its shutdown sequence it will start all registered shutdown hooks in some unspecified order and let them run concurrently.
public class ShutDownHookEx {
 public static void main(String args[]) {
  Runtime.getRuntime().addShutdownHook(new Thread() {
   @Override
   public void run() {
    System.out.println("ShutDown Hook1");
   }
  });
  
  Runtime.getRuntime().addShutdownHook(new Thread() {
   @Override
   public void run() {
    System.out.println("ShutDown Hook2");
   }
  });
  
  Runtime.getRuntime().addShutdownHook(new Thread() {
   @Override
   public void run() {
    System.out.println("ShutDown Hook3");
   }
  });

  System.out.println("About to Exit the Application");
  System.exit(0);
  System.out.println("Application Exited");
 }

}

Sample Output
About to Exit the Application
ShutDown Hook1
ShutDown Hook3
ShutDown Hook2


Is there any way that I can skip these shutdown hooks?
By using the halt method of Runtime class, you can skip the shutdown hooks. ‘halt’ method forcibly terminates the currently running Java virtual machine.

public class ShutDownHookEx {
 public static void main(String args[]) {
  Runtime.getRuntime().addShutdownHook(new Thread() {
   @Override
   public void run() {
    System.out.println("ShutDown Hook1");
   }
  });

  Runtime.getRuntime().addShutdownHook(new Thread() {
   @Override
   public void run() {
    System.out.println("ShutDown Hook2");
   }
  });

  Runtime.getRuntime().addShutdownHook(new Thread() {
   @Override
   public void run() {
    System.out.println("ShutDown Hook3");
   }
  });

  System.out.println("About to Exit the Application");
  Runtime.getRuntime().halt(0);
  System.out.println("Application Exited");
 }

}


Output
About to Exit the Application

Can I stop the shutdown sequence in middle of processing?
Yes any time by calling the halt method.

Can I register (or) deregister shutdown hooks,after shutdown sequence begun?
No, once the shutdown sequence has begun it is impossible to register a new shutdown hook or de-register a previously-registered hook. Attempting either of these operations will cause an IllegalStateException to be thrown.

Can I perform long-running tasks in shutdown hook?
Ideally no, you are not supposed to perform long running operationg in shutdown hook, it is because when the virtual machine is terminated due to user logoff or system shutdown the underlying operating system may only allow a fixed amount of time in which to shut down and exit. It is therefore inadvisable to attempt any user interaction or to perform a long-running computation in a shutdown hook.

Is there any cases, where shutdown hook processes will not execute?
Yes, following are the cases.
a. When you call halt method
b. When JVM is terminated with the SIGKILL signal on Unix or the TerminateProcess call on Microsoft Windows.
c. If JVM aborts, due to some internal errors.

How to deregister shutdown hook?
By calling 'removeShutdownHook' method, you can deregister shutdown hook.
public boolean removeShutdownHook(Thread hook)






No comments:

Post a Comment