Java tutorial
class Message extends Thread { public void run() { System.out.println("Bye."); } } public class Main { public static void main(String[] args) { try { Message p = new Message(); // register Message as shutdown hook Runtime runTime = Runtime.getRuntime(); runTime.addShutdownHook(p); // remove the hook runTime.removeShutdownHook(p); System.out.println("Program is closing..."); } catch (Exception e) { e.printStackTrace(); } } }