Add exit event processing for your Java application

Source: Internet
Author: User

Author: Li luqun


Author Profile

Li luqun, male, associate professor. He is mainly engaged in the research and teaching of Java J2EE and j2's integrated applications, spatial web service, and mobile GIS applications. You can contact the author via liluqun@263.net.

Body

A complete Java application usually requires at least one end point of the application. For general programs, system developers add system at the end of the program based on their needs and personal preferences. exit (0), or system. out (-1), to end the program, or without these commands, so that the program runs until the end.

For example, the following typical code

Package untitled14;/*** this application is to demo how an applcation end */public class test {public test () {} public static void main (string [] ARGs) {test test1 = new test ();//................. system. out. println ("Hello World"); // do something before system exit system. exit (0); // you can leave this code to end the program. }}

For a simple application system, we can directly. before executing the exit (0) code, add the work that needs to be completed before the application exits, such as closing the network connection and closing the database connection.

However, for complicated multi-threaded applications, the thread running status is complex, so it is hard to predict when the program will end and how to end the event when the application ends, what should we do? This uses Java's event sourcing mechanism for application exit.

Java uses the runtime static method to obtain the current application object: runtime. getruntime () registers a shutdown hook event to the Java Virtual Machine through the void addshutdownhook (thread hook) method of the runtime, so that once the program ends the event, it runs the thread hook. in actual application, as long as the program needs to complete some of the previously done work directly through the thread hook to complete. The Demo code is as follows:

/*************************************** * *********************************** This program demonstration only, how to add a system exit event processing mechanism **************************** **************************************** * *******/package untitled14; import Java. util. *; import Java. io. *;/*** this application is used to demo how to hook the event of an application */public class untitled1 {public untitled1 () {doshutdownwork ();} /*********** **************************************** * ********************** This is the right work that will do before the system shutdown * here demo, adds an event processing for the exit of the application. * When the application exits, it writes the exit date to D:/t. log File ************************************** * *********************************/private void doshutdownwork () {runtime. getruntime (). addshutdownhook (New thread () {public void run () {try {filewriter fw = new fi Lewriter ("D: // T. log"); system. Out. println ("I'm going to end"); FW. Write ("the application ended! "+ (New date ()). tostring (); FW. close () ;}catch (ioexception ex ){}}});} /*************************************** * ************* this is the entry to the program, only for demonstration, the code in the method does not matter *********************************** * **************/public static void main (string [] ARGs) {untitled1 untitled11 = new untitled1 (); long s = system. currenttimemillis (); For (INT I = 0; I <1000000000; I ++) {// Add the code you need to process here} Long Se = system. currenttimemillis (); system. out. println (Se-S );}}

In the above program, we can see that by adding runtime in the program. getruntime (). addshutdownhook (New thread () event monitoring, capture the arrival of the system Exit message, and then execute what we need to do to make our program more robust!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.