程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> 解析Runtime中shutdown hook的使用詳解

解析Runtime中shutdown hook的使用詳解

編輯:JAVA編程入門知識
根據 Java API, 所謂 shutdown hook 就是已經初始化但尚未開始執行的線程對象。在Runtime 注冊後,如果 jvm 要停止前,這些 shutdown hook 便開始執行。聲明:Runtime.addShutdownHook(Thread t)
舉例如下:
代碼如下:

package john2;
 
/**
 * 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會不會占用太多的VM的資源,答案是shutdown hook不會占用VM太多的資源,因為shutdown hook 只是一個已初始化但尚未啟動的線程。這意味著它只在程序關閉的時候才會啟動,而不是在程序一開始運行時就啟動。而在大多數的Java平台中,如果一個線程沒有啟動(即沒有調用線程的start()函數)VM不會分配資源給線程。因此維護一群沒有啟動的線程不會給VM帶來太大的負擔.
最後還要注意以下兩點:
如果VM crash,那麼不能保證關閉掛鉤(shutdown hooks)能運行.試想一下如果Windows XP突然藍屏了那麼本來計劃在關機之前的更新也就無法進行了.
如果調用Runtime.halt()方法來結束程序的話,那麼關閉掛鉤(shutdown hooks)也不會執行
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved