当关机挂钩坏了 [英] When Shutdown Hooks Break Bad

查看:120
本文介绍了当关机挂钩坏了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将 shutdown hook 添加到我的Java程序的运行时,如下所示:

If I add a shutdown hook to my Java program's runtime like so:

public class MyShutdownHook implements Runnable
{
    @Override
    public void run()
    {
        // Stuff I want executed anytime
        // the program, Java, or the OS exits normally,
        // crashes, or terminates unexpectedly for any reason.
    }
}

// The in another method...
Runtime.getRuntime().addShutdownHook(new MyShutdownHook());

...然后 任何情况下 run()当程序/ Java / OS正常退出,崩溃或意外终止时,方法将不会执行?如果是这样,什么情况可以绕过运行时的关闭钩子,为什么?

...then are there ever any situations where that run() method won't execute when the program/Java/OS exits normally, crashes or terminates unexpectedly? If so, what situations would be able to by-pass Runtime's shutdown hook, and why?

推荐答案


  • 如果进程被终止,则不会执行关闭挂钩。

    • If the process is killed, a shutdown hook will not be executed.

      如果进程崩溃,将不会执行关闭挂钩。

      If the process crashes, a shutdown hook will not be executed.

      如果你有一个Windows服务,并且关闭钩子需要很长时间才能执行,它将被终止。

      If you have a Windows service and the shutdown hook takes to long to execute, it will be terminated.

      这篇关于当关机挂钩坏了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆