开发者

Intercepting java machine shutdown call?

开发者 https://www.devze.com 2023-02-01 15:42 出处:网络
Is there any way to get notified of JVM shutting do开发者_Go百科wn or System.exit call before the actual shutdown? I want this, so my application will be able to make a clean exit.

Is there any way to get notified of JVM shutting do开发者_Go百科wn 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?


Have a look at Runtime.addShutdownHook

From the API docs:

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

  • The program exits normally, when the last non-daemon thread exits or when the exit (equivalently, System.exit) method is invoked, or
  • The virtual machine is terminated in response to a user interrupt, such as typing ^C, or a system-wide event, such as user logoff or system shutdown.

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


There's Runtime.getRuntime().addShutdownHook() method that you might want to explore.

0

精彩评论

暂无评论...
验证码 换一张
取 消