拦截java机关机调用? [英] Intercepting java machine shutdown call?

查看:160
本文介绍了拦截java机关机调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在实际关闭之前收到JVM关闭或System.exit调用的通知?我想要这个,所以我的应用程序将能够彻底退出。
我意识到这样的事情不太可能存在,但是,也许有这样的事情?

Is there any way to get notified of JVM shutting down or System.exit call before the actual shutdown? I want this, so my application will be able to make a clean exit. I realize that such thing is unlikely to exist, but still, maybe there is something like this?

推荐答案

有看看 Runtime.addShutdownHook

来自API文档:

Java虚拟机关闭对两种事件的响应:

The Java virtual machine shuts down in response to two kinds of events:


  • 当最后一个非守护程序线程退出或退出时(等效于系统),程序正常退出调用.exit)方法,或

  • 虚拟机终止以响应用户中断,例如键入^ C或系统范围的事件,例如用户注销或系统shutdown。

使用 Runtime.addShutdownHook 您可以在这些情况下运行代码。

Using Runtime.addShutdownHook you can run code in those cases.

这篇关于拦截java机关机调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆