public class Main {
    static {
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                System.out.println("Bye, bye JVM :(");
            }
        });
    }
    
    public static void main(String[] args) {
        System.out.println("Doing something....");
        try {
            Thread.sleep(100000);
        } catch (Exception e) {
        } 
    }
}
Make sure to add the shutdown hook in the static block.
Friday, September 17, 2010
JVM Shutdown Hook
Suppose our Java application receives a SIGINT or SIGKILL signal and we need to do something before the JVM terminates. Java provides a way to add a shutdown hook into the JVM.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment