当前位置: 移动技术网 > IT编程>开发语言>Java > 解析Runtime中shutdown hook的使用详解

解析Runtime中shutdown hook的使用详解

2019年07月22日  | 移动技术网IT编程  | 我要评论
根据 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)也不会执行

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网