解析Runtime中shutdown hook的使用详解

2016-02-19 09:38 15 1 收藏

今天图老师小编给大家介绍下解析Runtime中shutdown hook的使用详解,平时喜欢解析Runtime中shutdown hook的使用详解的朋友赶紧收藏起来吧!记得点赞哦~

【 tulaoshi.com - 编程语言 】

根据 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)也不会执行

来源:https://www.tulaoshi.com/n/20160219/1591544.html

延伸阅读
一、简单介绍 1.什么是UIPopoverController 是iPad开发中常见的一种控制器(在iPhone上不允许使用) 跟其他控制器不一样的是,它直接继承自NSObject,并非继承自UIViewController 它只占用部分屏幕空间来呈现信息,而且显示在屏幕的最前面 2.使用步骤 要想显示一个UIPopoverController,需要经过下列步骤 (1)设置内容控制器 ...
标签: 服务器
linux中less命令使用详解   less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大。less 的用法比起 more 更加的有弹性。在 more 的时候,我们并没有办法向前面翻, 只能往后面看,但若使用了 less 时,就可以使用 [pageup] [pagedown] 等按键的功能来往前往后翻看文件,更容易用...
标签: 服务器
linux中which命令使用详解   我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索: which 查看可执行文件的位置。 whereis 查看文件的位置。 locate 配合数据库查看文件位置。 find 实际搜寻硬盘查询文件名称。 which命令的作用是,在PATH变量指定的路径中,搜索某个系统命...
内部类访问规则 •内部类可以直接访问外部类中的成员,包括私有。访问格式:外部类名.this •外部类要访问内部类必须创建内部类对象。 •内部类在成员位置上,可以被成员修饰符修饰。 代码如下: public class InnerClassDemo1 {      public static void main(String[] args){    &nbs...
一、Keychain 基础 根据苹果的介绍,iOS设备中的Keychain是一个安全的存储容器,可以用来为不同应用保存敏感信息比如用户名,密码,网络密码,认证令牌。苹果自己用keychain来保存Wi-Fi网络密码,VPN凭证等等。它是一个sqlite数据库,位于/private/var/Keychains/keychain-2.db,其保存的所有数据都是加密过的。 开发者通常会希望能够利用...

经验教程

729

收藏

95
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部