Add exit Event Response for Java applications

Source: Internet
Author: User

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 only demonstrates how to add a system exit event processing mechanism to a Java application.
**************************************** *************************************/
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
* For demonstration, an event is added for exiting the application,
* When the application exits, write the program exit date to the d: \ t. log file.
**************************************** **********************************/
Private void doShutDownWork (){
Runtime. getRuntime (). addShutdownHook (new Thread (){

Public void run (){
Try {
FileWriter fw = new FileWriter ("d: \ t. log ");
System. out. println ("Im going to end ");
Fw. write ("the application ended! "+ (New Date (). toString ());
Fw. close ();
}
Catch (IOException ex ){
}

}
});
}

/*************************************** *************
* This is the entry to the program and is only for demonstration. The code in the method is irrelevant.
**************************************** ***********/

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
}
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!

Related Article

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.