When Shutdown Hooks Break Bad

我只是一个虾纸丫 提交于 2019-12-01 15:53:38
  • 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.

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.

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.

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)

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!