How to use a swing in a disconnect hook?

Is there any possible way to add rocking to the shutdown hook (i.e. to display a popup when VM shuts down)?

I understand that if I try to create a new JFrame, it will give me an error, because it is trying to register a disconnect hook, which does not work, since the VM is already disconnecting. I'm just wondering if there is anything like that at all

+6
source share
5 answers

You really shouldn't do that. From the Runtime.addShutdownHook specification :

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

  • A program terminates normally when the last non-daemon thread terminates or when the exit method is called (equivalently, System.exit ), or
  • The virtual machine terminates in response to a user interruption, for example, ^C input or a system-wide event, such as logging out or shutting down the system.

...

Shutdown hooks start at a difficult time in the life cycle of a virtual machine and therefore must be encoded in defense. They should, in particular, be written as thread safe and avoid deadlocks as much as possible. Neither should they blindly rely blindly on services that could register their own disconnection hooks and, therefore, can themselves in the shutdown process. For example, attempts to use other thread-based services, such as an AWT event flow, may result in deadlocks.

Shutdown hooks should also shut down quickly. When the program calls exit , the expectation is that the virtual machine will quickly shut down and exit. When a virtual machine shuts down due to a user logging off or shutting down the system, the base operating system can only allow a fixed period of time during which it is necessary to shut down and log off. Therefore, it is impractical to try any interaction with the user or to perform long-term calculation in the stop cache.

...

In rare cases, a virtual machine can interrupt work, that is, stop working without turning it off. This happens when the virtual machine shuts down externally, for example, with a SIGKILL signal on Unix or a TerminateProcess call on Microsoft Windows. In addition, the virtual machine may interrupt if the native method goes awry, for example, corrupting internal data structures or trying to access non-existent memory. If the virtual machine is interrupted, then there can be no guarantee as to whether any shutdowns will be performed.

Specific warnings here that suggest that you do not:

  • "Finishing hooks should also finish their work quickly."

    Relying on everything that may take some time to complete your work, or blocking unlimited time on user inputs such as JOptionPane dialogs, is not what you should do in your shutdown. p>

  • "Attempts to use other thread-based services, such as AWT event flow, for example, can lead to deadlocks"

    Swing runs on top of AWT, whose main event dispatch thread can also fail. Trying to use Swing or AWT when shutting down can not only lead to dead locks, but it may not work at all.

  • "If the virtual machine is interrupted, then there can be no guarantee as to whether any terminating hooks will be executed."

    There is no guarantee that your user can even receive your message, because the final hooks are guaranteed only when it is completed or completed - not when stopped or interrupted.

+13
source

Shutdown switches should run as quickly as possible. This does not mean that the user must confirm the dialog. In any case, you have no guarantee that the Swing event flow is still running.

You cannot do this.

+4
source
+2
source

If so, it will not help you.

Shutdown hooks are called asynchronously as part of the JVM termination, so the โ€œconfirmโ€ dialog will not confirm anything, since you cannot stop or cancel the shutdown process. Waiting for a user to make a decision is not the kind of action for which shutdown is intended. Closing in an interactive program does not make sense. Real use case for trip hooks:

to free up resources and other housekeeping when the JVM shuts down

It is also important to note that the shutter closes normally does not start, because for more details see my answer here: How to properly close a Java application with C # one

+2
source

I am not sure about your question, but I think it is impossible to launch it or display a popup when closing the JVM. Is this like trying to escape while preparing for bed? Just guessing. :)

-1
source

Source: https://habr.com/ru/post/923731/


All Articles