Running code on program exit in Java

一曲冷凌霜 提交于 2019-12-17 06:12:40

问题


Is it possible to write a method that System.exit will call when you terminate a program?


回答1:


Use Runtime.getRuntime().addShutdownHook(Thread).




回答2:


Shutdown hooks are the answer... here is an article on them. They do not come without issues (some of them are discussed in the article).




回答3:


You can use a shutdown hook.

http://download.oracle.com/javase/6/docs/api/java/lang/Runtime.html#addShutdownHook(java.lang.Thread)

Note that shutdown hooks will not run if the VM aborts abnormally or Runtime.halt(int) is called.




回答4:


You can add a VM shutdown hook.




回答5:


Look into shutdown hooks, see http://docs.oracle.com/javase/8/docs/technotes/guides/lang/hook-design.html



来源:https://stackoverflow.com/questions/5747803/running-code-on-program-exit-in-java

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