Questions tagged [shutdown-hook]

A shutdown hook is simply an initialized but unstarted thread. When the virtual machine begins its shutdown sequence it will start all registered shutdown hooks in some unspecified order and let them run concurrently.

public void addShutdownHook(Thread hook)

Registers a new virtual-machine shutdown hook.

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. A shutdown hook is simply an initialized but unstarted thread. When the virtual machine begins its shutdown sequence it will start all registered shutdown hooks in some unspecified order and let them run concurrently. When all the hooks have finished it will then run all uninvoked finalizers if finalization-on-exit has been enabled. Finally, the virtual machine will halt. Note that daemon threads will continue to run during the shutdown sequence, as will non-daemon threads if shutdown was initiated by invoking the exit method.

Once the shutdown sequence has begun it can be stopped only by invoking the halt method, which forcibly terminates the virtual machine.

Once the shutdown sequence has begun it is impossible to register a new shutdown hook or de-register a previously-registered hook. Attempting either of these operations will cause an IllegalStateException to be thrown.

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.

Uncaught exceptions are handled in shutdown hooks just as in any other thread, by invoking the uncaughtException method of the thread's ThreadGroup object. The default implementation of this method prints the exception's stack trace to System.err and terminates the thread; it does not cause the virtual machine to exit or halt.

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.

220 questions
131
votes
1 answer

Useful example of a shutdown hook in Java?

I'm trying to make sure my Java application takes reasonable steps to be robust, and part of that involves shutting down gracefully. I am reading about shutdown hooks and I don't actually get how to make use of them in practice. Is there a practical…
Jason S
  • 184,598
  • 164
  • 608
  • 970
28
votes
5 answers

debug_backtrace() from registered shutdown function in PHP

While tinkering for an answer to this question, I found that debug_backtrace() doesn't trace beyond the function registered to register_shutdown_function(), when called from within it. This was mentioned in this comment for…
Dan Lugg
  • 20,192
  • 19
  • 110
  • 174
27
votes
6 answers

Shutdown hook doesn't work in Eclipse

I have added a shutdown hook via: Runtime.getRuntime().addShutdownHook(myShutdownHook); It works fine normally, but not when I click the red stop button in Eclipse. Is there a way to make the shutdown hook be called in Eclipse?
urir
  • 1,960
  • 3
  • 23
  • 40
20
votes
1 answer

shutdown hook doesn't fire when running with "lein run"

I have the following code: (ns test-hook.core) (defn -main [] (.addShutdownHook (Runtime/getRuntime) (Thread. #(println "shutdown"))) (println "start") (doseq [i (range 1 6)] (Thread/sleep 1000) (println i))) and the following…
Samus_
  • 2,903
  • 1
  • 23
  • 22
19
votes
3 answers

Windows shutdown hook on java application run from a bat script

I have a bat script which runs a java application. If I press ctrl+c on it, it the application terminates gracefully, invoking all the shutdown hooks. However, if I just close the cmd window of the bat script, the shutdown hooks are never invoked.…
Zoltán
  • 21,321
  • 14
  • 93
  • 134
18
votes
2 answers

Cygwin CTRL-C (Signal Interrupts) not working properly - JVM Shutdown Hooks not starting

I'm working on a Java application that utilises shutdown hooks in order to clean up on termination/interruption of the program, but I've noticed that Cygwin's implementation of CTRL-C doesn't seem to trigger the shutdown hooks. On the surface it…
Quetzalcoatl
  • 3,037
  • 2
  • 18
  • 27
17
votes
5 answers

Run a script only at shutdown (not log off or restart) on Mac OS X

Is there any way to run a script only at shutdown? I mean, only when the computer is really shutting down to off state. This script should not run when doing just a log off or restart.
Audio01
  • 173
  • 1
  • 1
  • 5
16
votes
1 answer

Using JavaFX Application.stop() method over Shutdownhook

So im using shutdownhook to clean up. But since its not always guaranteed that shutdownhooks thread executes, should i just push this code onto JavaFX Application Thread (method stop()), which executes every time i close my application? Code is not…
Tomas Bisciak
  • 2,801
  • 5
  • 33
  • 57
16
votes
1 answer

How can I get IntelliJ debugger to allow my app's shutdown hooks to run?

When debugging in IntelliJ an app that registers shutdown hooks, the shutdown hooks do not get called if I click the green "restart" circular arrow button, nor if I click the red square "stop" button. In both cases it appears the app-under-debug is…
16
votes
2 answers

Java -How to get logger to work in shutdown hook?

I have a specialized logger class which uses the java.util.logging.Logger class. I want to be able to use this logger in the shutdown hook of another class. However, it doesn't seem to log at shutdown. From what I've read, there may already be a…
user1514879
14
votes
2 answers

How does CTRL-C work with Java program

When I press ctrl-c in console in what sequence are application threads stopped and shutdown hooks called?
user590444
  • 4,252
  • 8
  • 36
  • 43
13
votes
2 answers

Java annotation processing: how do I know if a round is the last one?

When extending AbstractProcessor, there is a possibility to override init(...), but there is no "opposite" method, which would be called after all rounds were processed. This is a problem: when you have to append information collected during each…
java.is.for.desktop
  • 10,748
  • 12
  • 69
  • 103
13
votes
1 answer

PHP's register_shutdown_function to fire when a script is killed from the command line?

Is it possible to invoke a function when a cron process is killed from the command line (via Ctrl+c) or with the kill command? I have tried register_shutdown_function(), but it doesn't seem to be invoked when the script is killed, but does get…
Wei
  • 135
  • 1
  • 5
13
votes
1 answer

Is there something like finally() in Go just opposite to what init()?

Is there something in Go which do just opposite to what init() do inside a package?
Prashant
  • 3,823
  • 3
  • 25
  • 40
13
votes
2 answers

shutdown hook vs finalizer method

I just fail to understand why must one use Runtime.addShutdownHook. If you want to do some cleanup when the jvm exits, why not just overload the finalize method of the daemon class. What is the advantage of using shutdown hook over finalize…
S Kr
  • 1,831
  • 2
  • 25
  • 50
1
2 3
14 15