Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You really shouldn't be doing this. From <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#addShutdownHook(java.lang.Thread)" rel="noreferrer">the <code>Runtime.addShutdownHook</code> specification</a>:</p> <blockquote> <p>The Java virtual machine <em>shuts down</em> in response to two kinds of events:</p> <ul> <li>The program <em>exits</em> normally, when the last non-daemon thread exits or when the <code>exit</code> (equivalently, <code>System.exit</code>) method is invoked, or</li> <li>The virtual machine is <em>terminated</em> in response to a user interrupt, such as typing <code>^C</code>, or a system-wide event, such as user logoff or system shutdown.</li> </ul> <p>...</p> <p>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.</p> <p>Shutdown hooks should also finish their work quickly. When a program invokes <code>exit</code> 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.</p> <p>...</p> <p>In rare circumstances the virtual machine may <em>abort</em>, that is, stop running without shutting down cleanly. This occurs when the virtual machine is terminated externally, for example with the <code>SIGKILL</code> signal on Unix or the <code>TerminateProcess</code> 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.</p> </blockquote> <p>Specific warnings here that suggest you <em>not</em> do this:</p> <ol> <li><p><em>"Shutdown hooks should also finish their work quickly."</em></p> <p>Relying on <em>anything</em> that might take a while to do its work, or blocking indefinitely on user-input like <code>JOptionPane</code> dialogs, is <strong>not</strong> what you should be doing in your shutdown hook.</p></li> <li><p><em>"Attempts to use other thread-based services such as the AWT event-dispatch thread, for example, may lead to deadlocks"</em></p> <p>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.</p></li> <li><p><em>"If the virtual machine aborts then no guarantee can be made about whether or not any shutdown hooks will be run"</em></p> <p>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 -- <strong>not</strong> when halted or aborted.</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload