Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use swing in a shutdown hook?

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

like image 716
Alex Coleman Avatar asked Aug 25 '12 07:08

Alex Coleman


4 Answers

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.

like image 138
obataku Avatar answered Nov 05 '22 14:11

obataku


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.

like image 41
user207421 Avatar answered Nov 05 '22 16:11

user207421


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

like image 2
David Kroukamp Avatar answered Nov 05 '22 16:11

David Kroukamp


  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)

like image 2
mKorbel Avatar answered Nov 05 '22 14:11

mKorbel