posted 14 years ago
Shutdown hooks run when the JVM would otherwise shut down. The JVM will not shut down while non-daemon threads remain alive, unless a specific event causes a shutdown, such as System.exit(), ctrl-C from a user, a kill command from the system, or something similar.
Note that shutdown threads are themselves typically non-daemon threads - but it isn't started until the shutdown sequence begins. So what happens is, in a normal exit, the JVM waits until all previously-started non-daemon threads complete, then it starts a new thread (typically non-daemon) for each shutdown hook that has been added. Once all these complete, the system exits. So there can be one or more non-daemon threads after shutdown begins, but those are only the shutdown hooks themselves. None of the other previously-running non-daemon threads may still be alive at this point.
"I'm not back." - Bill Harding, Twister