Skip to main content

ShutDownHook - the last breath of an application

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)
Below is an example which will start endless loop which do nothing. But an important thing in this code is a part where shutDownHook is added. When an termination signal will be send to JVM a code from a run() method will be executed just before JVM shuts down.
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

Popular posts from this blog

scala command line tools

scalac scala compiler // compile a file scalac filename.scala fsc fast scala compiler (scala daemon for faster compiling) // start scala daemon and compile a file fsc filname.scala // stop scala deamon fsc -shutdown scala scala interpreter // run interpreter scala filename.scala

sqlplus used in perl

sqlplus is a command line tool used to work with an Oracle database. #!/bin/perl my $sqlString ='sqlplus -s user/pass@SID usage: sqlplus [-s] user/pass@SID -s parameter enables a silent mode. Only result of statement executions will be returned to a standard output (data like 'SQL>' prompt or a connection confirmation will be skipped) To disable a default human-redable format of returned data the following parameters are set: heading, echo, feedback, pagesize.