Sometimes there is important stuff that needs to be done when the user stops the JVM.  For example, some important files may need to be closed, some important calculations may need to be closed, etc.

public class ShutdownManager
{
    static
    {
        Thread cleanupThread = new Thread("JVM Shutdown Thread")
        {
            public void run()
            {
                systemShutdown();
            }
        };
        // register the cleanup thread with the JVM
        Runtime.getRuntime().addShutdownHook(cleanupThread);
    }
:

    public static void systemShutdown()
    {
        // developer code: shut down in reverse order to startup
        if (engine != null)
        {
            engine.stop();
            engine = null;
        }
    }
}

Exception: Shutdown methods are only moderately useful because they do not get called before power outages and hardware failures.  Therefore, all software must assume that a power failure (also known as a hard reboot) can occur at any time and should recover from it cleanly.

blog comments powered by Disqus