When Shutdown Hooks Break Bad

前端 未结 5 579
暖寄归人
暖寄归人 2021-01-17 16:53

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

public class MyShutdownHook implements Runnable
{
    @Override
    public void run(         


        
相关标签:
5条回答
  • 2021-01-17 17:26

    Any time the JVM can exit gracefully, the shutdown hook is run. It is not run if:

    • The JVM crashes. (Depending on how/when it crashes, the hook may still run, but no guarantees are made)
    • The JVM gets SIGKILL or similar, and is immediately stopped by the OS
    0 讨论(0)
  • 2021-01-17 17:30
    • If the process is killed, a shutdown hook will not be executed.

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

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

    0 讨论(0)
  • 2021-01-17 17:31

    If you pull the plug out the back of your computer then the shut-down hook won't run. Other situations include:

    • The process being killed on a POSIX OS with SIGKILL (-9).
    • Similarly if you elect to terminate an "unresponsive" process under Windows the shut-down hook may not complete.
    0 讨论(0)
  • 2021-01-17 17:41

    They only run if the process "shuts down normally." If the process is killed forcefully from the OS or if it's crashing due to resource issues (e.g., out of memory), shutdown hooks won't be invoked.

    0 讨论(0)
  • 2021-01-17 17:44

    when you do Runtime.getRuntime().halt() the JVM or when the OS kills the process without allowing it to run any cleanup, also when you have eclipse terminate the program (when running it under eclipse)

    0 讨论(0)
提交回复
热议问题