import sun.misc.Signal; import sun.misc.SignalHandler; public class ExampleSignalHandler { public static void main(String... args) throws InterruptedException { final long start = System.nanoTime(); Signal.handle(new Signal("TERM"), new SignalHandler() { public void handle(Signal sig) { System.out.format("\nProgram execution took %f seconds\n", (System.nanoTime() - start) / 1e9f); System.exit(0); } }); int counter = 0; while(true) { System.out.println(counter++); Thread.sleep(500); } } }