Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Shutdown hook not called when hitting ctrl-c

843829Dec 12 2005 — edited Dec 12 2005
Hello

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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 9 2006
Added on Dec 12 2005
3 comments
520 views