Add exit event handling for your Java application

Source: Internet
Author: User
Tags add date exit end event listener log string thread
Program

A complete Java application that usually has at least one end point for the application. For a general program, the system developer ends the program at the end of the program by adding System.exit (0), or System.out (-1), depending on the needs and preferences of the individual, or without these instructions, allowing the program to end naturally.

Such as: The following typical code

Package untitled14;/** * This application are to demo, 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 not write this code, let the program end naturally.      }}

For simple application systems, we can add work that needs to be done before System.exit (0) code is executed, such as shutting down the network connection, shutting down the database connection, and so on.

However, for more complex multi-threaded applications, where threads run more complex states, it is difficult to anticipate when the program will end and how to handle the work we do when the application's End event arrives. This uses the Java event-handling mechanism to exit the application.

Access to the current Application object, Java through the Runtime static method: Runtime.getruntime () through the Runtime void Addshutdownhook (Thread hook) Method registers a shutdown hook event with the Java Virtual machine so that once the program's end event arrives, it runs the thread hook, and in practice, we simply do some of the work done before the program needs to be done directly through the thread hook. The specific demo code is as follows:

/***************************************************************************** This program only demonstrates how to add a system exit event handling mechanism to a Java application * /package Untitled14;import Java.util . *;import java.io.*;/** * This application are used to demo how to hook the event of a application */public class untitled  1 {public Untitled1 () {doshutdownwork (); }/*************************************************************************** * The right work that'll do Fore the system shutdown * Here for demonstration, add an event handler for the exit of the application, * write the date of the program exit to the D:\t.log file when the application exits ***************************** /private void Doshutdownwork () {runtime.getruntime (). Addshutdownhook          (New Thread () {public void run () {try {FileWriter fw = new FileWriter ("D:\\t.log");          System.out.println ("I ' m going to end");          Fw.write ("The Application ended!" + (new Date ()). ToString ()); Fw.closE ();  The catch (IOException ex) {}}}); /**************************************************** * This is the entrance to the program, for demonstration only, 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 you need to process code here} long SE = System.currenttimemillis ();  System.out.println (se-s); }}

In the above program, we can see that by adding the Runtime.getruntime (). Addshutdownhook (new Thread) event listener in the program, capturing the system exit message arrives, and then executing what we need to do to get the job done, So that our program is 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.