ShutDownHook allows you to perform operations (e.g. close opened resources, remove temporary files and so on) just before virtual machine shuts down. A documentation says that JVM may shut down in two cases:
- program finishes execution normally when all threads finishes their work (except deamon-threads like garbage collector)
 - virtual machine receives a termination signal (for example after sending kill signal under unix or ctrl + C key combination under windows)
 
public class ShutDownHook {
 public static void main(String[] args) {
  Runtime.getRuntime().addShutdownHook(new Thread() {
   public void run() {
    System.out.println("Close opened resources");
   }
  });
  while (true) {
   // do nothing
  }
 }
}
Comments
Post a Comment