Intercepting java machine shutdown call?

前端 未结 2 853
我在风中等你
我在风中等你 2021-01-18 21:31

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 rea

2条回答
  •  滥情空心
    2021-01-18 22:04

    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.

提交回复
热议问题