Runtime.getRuntime().addShutdownHook(new Thread()

  • 摘要:为您的Java应用程序添加退出事件处理--------------------------------------------------------------------------------一个完整的Java应用程序,通常至少要有一个应用程序的结束点。对于一般程序来说,系统开发者根据需要和个人的偏好,会在程序结束位置,通过添加System.exit(0),或System.out(-1),来结束程序,或不加这些指令,让程序自然运行到结束。如:下列典型代码packageu

  • 为您的Java应用程序添加退出事件处理

    --------------------------------------------------------------------------------

    一个完整的Java应用程序,通常至少要有一个应用程序的结束点。对于一般程序来说,系统开发者根据需要和个人的偏好,会

    在程序结束位置,通过添加System.exit(0),或System.out(-1),来结束程序,或不加这些指令,让程序自然运行到结束。

    如:下列典型代码

    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);//也可以不写这句代码,让程序自然结束。
        
    }
    }

    对于简单的应用系统,我们直接可以在System.exit(0)代码执行前,添加需要在应用程序退出前需要完成的工作,如:关闭网

    络连接,关闭数据库连接等。

    然而,对于比较复杂的多线程应用,线程运行的状态较复杂,我们就很难预料程序何时结束,如何能在应用程序结束事件到来

    时,处理我们要做的工作呢?这就用到了Java对应用程序的退出的事件出处理机制。

    对当前应用程序对象的获得,Java通过Runtime静态方法:Runtime.getRuntime()通过Runtime的 void addShutdownHook

    (Thread hook) 法向Java虚拟机注册一个shutdown钩子事件,这样一旦程序结束事件到来时,就运行线程hook,我们在实际应

    用时候,只要将程序需要完成之前做的一些工作直接通过线程hook来完成。具体演示代码如下:

    /*****************************************************************************
    本程序仅演示,如何在Java应用程序中添加系统退出事件处理机制
    *****************************************************************************/
    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
       * 这里为了演示,为应用程序的退出增加了一个事件处理,
       * 当应用程序退出时候,将程序退出的日期写入 d:/t.log文件
       **************************************************************************/
    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();
            }
            catch (IOException ex) {
            }

          }
        });
    }
    /****************************************************
       * 这是程序的入口,仅为演示,方法中的代码无关紧要
       ***************************************************/
    public static void main(String[] args) {
        Untitled1 untitled11 = new Untitled1();
        long s = System.currentTimeMillis();
        for (int i = 0; i < 1000000000; i++) {
        //在这里增添您需要处理代码

        }
        long se = System.currentTimeMillis();

        System.out.println(se - s);
    }
    }

     


    在上述程序中,我们可以看到通过在程序中增加Runtime.getRuntime().addShutdownHook(new Thread()) 事件监听,捕获系统

    退出消息到来,然后,执行我们所需要完成工作,从而使我们的程序更健壮!

     

    通常情况下,我们一般调用System.exit()方法来退出JVM,查看System.exit()的设计可以发现这个方法调用了

    Runtime.getRuntime()的exit()方法,参考Runtime类结构我们可以得到关于系统退出时有关更多的方法。
    exit()方法会使java JVM退出,在Jdk1.3中,如果使用addShutdownHook()方法注册了一个线程,当通过调用exit()或通过用户

    中断(CTRL C)被关闭后,该线程将被激活调用,可以利用这一功能来在系统退出或异常退出捕捉这一时刻,做一些必要的退出

    操作。
    shutdownhook(关机钩)的主要目的是在系统中断后进行必要的清除,例如进行网络关闭、关闭打开的文件等操作,可以通过

    addShutdownHook()方法注册了一个这样的关机钩,并且允许你注册多个关机钩。在JVM退出之前,它会启动所有已注册的关机

    钩,并让这些关机钩线程同步执行。在一个关机钩执行之前可以使用removeShutdownHook()来删除一个已注册的关机钩,也可

    以调用halt()不调用关机钩线程直接退出JVM。

    下面是注册关机钩的例子,在addShutdownHook方法里构造了一个局部类,这个局部类实现了在系统中断退出时要执行的一些必

    要操作。在例子里,同时注册了两个关机钩。


    import java.lang.*;

    public class TestExit{
            public static void main(String[] args){
                    System.out.println("my java process");
                    //注册一个关机钩,当系统被退出或被异常中断时,启动这个关机钩线程
                    Runtime.getRuntime().addShutdownHook(new Thread(){                                        
                                            public void run(){ 
                                                    //添入你想在退出JVM之前要处理的必要操作代码
                                                    System.out.println("T1");}
                                    });
                    //注册第二个关机钩                
                    Runtime.getRuntime().addShutdownHook(new Thread(){
                                            public void run(){ System.out.println("T2");}
                                    });

                    System.exit(0);
            }
            
            
    }

    当测试这段代码时,系统可能输出结果如下:

    my java process
    T2
    T1

    原来,这两个关机钩线程在程序退出被JVM并行执行,如果你设置了线程优先级,将先执行一个高优先级的钩子线程,否则将被

    随机并行执行。


    package Thread;

    /** 
    * test shutdown hook 
    * All rights released and correctness not guaranteed. 
    */ 
    public class ShutdownHook implements Runnable { 
         
        public ShutdownHook() { 
            // register a shutdown hook for this class. 
            // a shutdown hook is an initialzed but not started thread, which will get up and run 
            // when the JVM is about to exit. this is used for short clean up tasks. 
            Runtime.getRuntime().addShutdownHook(new Thread(this)); 
            System.out.println(">>> shutdown hook registered"); 
        } 
         
        // this method will be executed of course, since it's a Runnable. 
        // tasks should not be light and short, accessing database is alright though. 
        public void run() { 
            System.out.println("/n>>> About to execute: " + ShutdownHook.class.getName() + ".run() to clean up before JVM exits."); 
            this.cleanUp(); 
            System.out.println(">>> Finished execution: " + ShutdownHook.class.getName() + ".run()"); 
        } 
         
            // (-: a very simple task to execute 
        private void cleanUp() { 
            for(int i=0; i < 7; i++) { 
                System.out.println(i); 
            } 
        }

        /** 
         * there're couple of cases that JVM will exit, according to the Java api doc. 
         * typically: 
         * 1. method called: System.exit(int) 
         * 2. ctrl-C pressed on the console. 
         * 3. the last non-daemon thread exits. 
         * 4. user logoff or system shutdown. 
         * @param args 
         */ 
        public static void main(String[] args) { 
             
            new ShutdownHook(); 
             
            System.out.println(">>> Sleeping for 5 seconds, try ctrl-C now if you like."); 
             
            try { 
                Thread.sleep(5000);     // (-: give u the time to try ctrl-C 
            } catch (InterruptedException ie) { 
                ie.printStackTrace(); 
            } 
             
            System.out.println(">>> Slept for 10 seconds and the main thread exited."); 
        }

    }

    /**
    >>> shutdown hook registered
    >>> Sleeping for 5 seconds, try ctrl-C now if you like.
    >>> Slept for 10 seconds and the main thread exited.

    >>> About to execute: Thread.ShutdownHook.run() to clean up before JVM exits.
    0
    1
    2
    3
    4
    5
    6
    >>> Finished execution: Thread.ShutdownHook.run()


    本文转自

以上是Runtime.getRuntime().addShutdownHook(new Thread()的内容,更多 的内容,请您使用右上方搜索功能获取相关信息。

猜你喜欢

转载自blog.csdn.net/qinhun0106/article/details/80801849
今日推荐