How do I use swing in a shutdown hook?

你说的曾经没有我的故事 提交于 2021-02-07 03:00:15

问题


Is there any possible way to add swing into a shutdown hook (that is, display a popup upon the VM shutting down)?

I realize that if I try to make a new JFrame, it will give me an error, as it tries to register a shutdown hook, which fails as the VM is already shutting down. I'm just wondering if there is in fact any way around this


回答1:


You really shouldn't be doing this. From the Runtime.addShutdownHook specification:

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.

...

Shutdown hooks run at a delicate time in the life cycle of a virtual machine and should therefore be coded defensively. They should, in particular, be written to be thread-safe and to avoid deadlocks insofar as possible. They should also not rely blindly upon services that may have registered their own shutdown hooks and therefore may themselves in the process of shutting down. Attempts to use other thread-based services such as the AWT event-dispatch thread, for example, may lead to deadlocks.

Shutdown hooks should also finish their work quickly. When a program invokes exit the expectation is that the virtual machine will promptly shut down and exit. When the virtual machine is terminated due to user logoff or system shutdown the underlying operating system may only allow a fixed amount of time in which to shut down and exit. It is therefore inadvisable to attempt any user interaction or to perform a long-running computation in a shutdown hook.

...

In rare circumstances the virtual machine may abort, that is, stop running without shutting down cleanly. This occurs when the virtual machine is terminated externally, for example with the SIGKILL signal on Unix or the TerminateProcess call on Microsoft Windows. The virtual machine may also abort if a native method goes awry by, for example, corrupting internal data structures or attempting to access nonexistent memory. If the virtual machine aborts then no guarantee can be made about whether or not any shutdown hooks will be run.

Specific warnings here that suggest you not do this:

  1. "Shutdown hooks should also finish their work quickly."

    Relying on anything that might take a while to do its work, or blocking indefinitely on user-input like JOptionPane dialogs, is not what you should be doing in your shutdown hook.

  2. "Attempts to use other thread-based services such as the AWT event-dispatch thread, for example, may lead to deadlocks"

    Swing runs on-top of AWT, whose underlying event-dispatch thread may be in the process of shutting down, too. Trying to use Swing or AWT while shutting down can lead not only to dead locks but also may just not work at all, anyways.

  3. "If the virtual machine aborts then no guarantee can be made about whether or not any shutdown hooks will be run"

    There are no guarantees your user could even possibly get your message, since shutdown hooks are only guaranteed to run when it exits normally or terminated -- not when halted or aborted.




回答2:


Shutdown hooks are supposed to execute as quickly as possible. That does not include waiting for a user to confirm a dialog. In any case you have no guarantee that the Swing event thread is still running.

You can't do this.




回答3:


If there is, it won't help you.

The shutdown hooks are invoked asynchronously as part of the JVM shutdown, so a "confirm" dialog won't really confirm anything as you can't halt or reverse the shutdown process. Waiting for a user to make a decision is not the kind of action a shutdown hook is meant for. A shutdown hook in an interactive program does not make sense. The real use case for shutdown hooks is:

for releasing resources and other housekeeping when the JVM shutsdown

It is also important to note the shut down hook wont always be run, for more see my answer here: How to shutdown java application correctly from C# one




回答4:


  1. Swing GUI must be done on Event Dispatch Thread, then

    • Create JDialog or JOptionPane on Initial Thread

    • Show Container

    • Call for Shutdown Hook

  2. Simple way, but required end user action (close JDialog)




回答5:


I'm not sure about your question, but I think its impossible to run or display a popup window when JVM is shutting down. Its like your trying to run while preparing to sleep? Just guessing. :)



来源:https://stackoverflow.com/questions/12120051/how-do-i-use-swing-in-a-shutdown-hook

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