How do I implement graceful termination in Java?

后端 未结 2 795
渐次进展
渐次进展 2021-02-20 02:09

Say for instance I have my application running in a Linux terminal and I press \"CTRL+C\" on my keyboard to kill the process it will terminate the Java program.

Is there

2条回答
  •  滥情空心
    2021-02-20 03:02

    I think the disclaimer is only there for kill -9, so that you don't rely on the shutdown hook being invoked to maintain say the consistency of your data.

    So if the process is allowed to act on the signal by the OS, the shutdown hook is invoked, which is pretty obvious if you know how an OS works, but maybe not to all Java developers.

    It is actually explained in great detail in the javadoc.

    Also worth bearing in mind is that there can be multiple shutdown hooks registered in a VM, so yours might not be THE shutdown hook, just one of several.

提交回复
热议问题