The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.lang  [58 examples]

e50. Determining When the Application Is About to Exit

When an application is terminated normally, the application first starts any registered shutdown threads, waits for them to complete and then finally exits. Normal termination can be caused by a call to System.exit(), the completion of the last non-daemon thread, or the interruption of the application (control-C) by the user. Abnormal termination (which does not cause the shutdown threads to be started) is caused some major fault in the Java virtual machine or native library.
    // Register a shutdown thread
    Runtime.getRuntime().addShutdownHook(new Thread() {
        // This method is called during shutdown
        public void run() {
            // Do shutdown work ...
        }
    });

 Related Examples
e48. The Quintessential Java Application
e49. Terminating the Application
e51. Computing Elapsed Time
e52. Loading Native Code
e53. Implementing a Class That Can Be Sorted
e54. Redirecting Standard Output, and Error
e55. Getting the Size of the Heap

See also: Arrays    Assertions    Classes    Commands    Numbers    Objects    Strings    System Properties    Threads   


© 2002 Addison-Wesley.