Shutdown hook not called when hitting ctrl-c
843829Dec 12 2005 — edited Dec 12 2005Hello
I was expecting to be able to do clean-up (using a shutdown hook) when my application was terminated by a ctrl-c with the following code. But if the process is busy doing something (and not sleeping as is the case in all examples I've been able to find) the VM will not run it's hooks, so it seems.
Am I missing something??
public class ShutdownHook
{
public static void main(String[] args)
{
Runtime.getRuntime().addShutdownHook( new Thread() { public void run() { System.out.println("shutdown"); }});
for (int j = 0; j < 10000; j++)
{
System.out.println("run");
// if the sleep is uncommented ctrl-c will work (99% of the time or so)
// Thread.sleep(100);
}
}
}
Regards,
Jesper