当关机挂钩坏了 [英] When Shutdown Hooks Break Bad
问题描述
如果我将 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屋!