Determine When Your Application Is About to Exit

     

What do you do when you want to shut down your Windows box? You click on Start. Likewise, when your application is shutting down, it doesn't stop at all. It rather starts something new. The application starts any registered shutdown threads. Only when they complete their work will the application exit. This is the case for normal termination ”for instance, when you call this code:

 

 System.exit(0); 

Passing the 0 int to the exit method means, by convention, that everything is hunky dory in your application, and that you just want to end it. Typically, a non-zero number (such as -1), as you might imagine, indicates a non-normal termination.

Abnormal termination is typically unexpected, but there are times when the chances are greater that something is going to run amok in your app. Like when you pass control over to something else, such as a native library or a network resource. The user can shut down the JVM abruptly by typing CTRL + C .

If there is some work that you would like to do but the application is about to shut down, you can register a shutdown thread, known as a hook, to do your dirty work. Maybe this dirty work is like, "write a message out to a log, log the user out, make sure he isn't working with any shared document keys, or send an e-mail." Some would argue that these things are perhaps too much work to do in a shutdown hook.

For example, we could write an entry to a log file when a user starts an application, and then call the shutdown hook to write the time when he shuts it down. This could give us possibly useful metrics on the order of, "How long did Sally do any work before goofing off on the Internet for 4.5 hours?" Here's how you do it.

ShutdownHook.java

 

 package net.javagarage.sys; /**  * <p>  * Demos the usefulness of the  * Runtime.getRuntime().addShutdownHook()  * method.  * @author eben hewitt  */ public class ShutdownHook { private void init(){ //register a new shutdown thread    Runtime.getRuntime().addShutdownHook(new Thread() {    //    public void run() {    //do work here System.out.println("In shutdown hook");    }    }); } private void doSomething(){ System.out.println("App started..."); } public static void main(String[] args) { ShutdownHook hook = new ShutdownHook(); hook.init(); hook.doSomething(); try { System.out.println("Sleeping..."); Thread.sleep(5000); } catch (InterruptedException ie){ System.out.println(ie.getMessage()); } } } 

Here are a few final considerations regarding shutdown hooks:

  • It is not guaranteed that the application will run your shutdown hooks. If the user unplugs the machine, for instance, they won't run. Also, if the process is terminated externally ”via a kill process command, for instance ”your hooks are not guaranteed to run.

  • Do little work in these hooks. The user will expect that your app will shut down cleanly and quickly when he requests it; it is good not to test his patience.

  • Code carefully in this method. This is obviously not a time to play fast and loose with things. The app is shutting down, and the JVM is shutting down; make sure your code here is thread safe.

That's enough about that. I'm going to watch some of the women's college volleyball championships. Oregon State is just demolishing Cal.



Java Garage
Java Garage
ISBN: 0321246233
EAN: 2147483647
Year: 2006
Pages: 228
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net