Saturday 7 July 2012

Java Shutdown Hooks

 A shut down hook is simply a Thread that is registered with the JVM and run just before the JVM shuts down.


Example code taken from the below mentioned link:




package example;public class ShutdownHook {

  
/**
   * 
@param args
   */
  
public static void main(String[] args) {
    
Hook hook = new Hook();
    System.out.println
"Running Main Application..." );
    Runtime.getRuntime
().addShutdownHookhook );
    System.out.println
"Exiting." );
  
}

  
  
private static class Hook extends Thread {
    
public void run() {
      
System.out.println"Running Clean Up..." );
    
}
  }
}



http://www.crazysquirrel.com/computing/java/basics/java-shutdown-hooks.jspx

http://stackoverflow.com/questions/2921945/useful-example-of-a-shutdown-hook-in-java

No comments:

Post a Comment