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
0
votes
1 answer

join() waiting forever when exception occurs, jvm shutdown hook not working

I am trying to shutdown the application, whenever any Fatal Error/Exception comes but before shut down the application my current thread/task should complete, so I have written mainThread.join() inside run(), its working fine when there is no…
0
votes
1 answer

Stop jvm from commandline for gracefull shutdown

I am doing some Poc for Stilts stomp server. Need to prevent server jvm termination and do it through another jvm. Here is a code snippet public static void main(String[] args) { try { log.debug("Starting server..."); …
vels4j
  • 11,208
  • 5
  • 38
  • 63
0
votes
1 answer

Java add shutdown hook inside method

In my code I'm using CompletionService and ExecutorService in order to start a bunch of Thread to execute some task (that can take a lot of time). So I have a method that creates the ExecutorService and the CompletionService, then starts submitting…
Raffaele
  • 461
  • 1
  • 7
  • 20
0
votes
1 answer

Flex ExternalInterface not firing in Safari (works everwhere else!)

I know there have been other questions on getting JavaScript / Flex ExternalInterface to play nicely, but I've what I think is a weird one. It works perfectly in every browser except Safari (on a Mac). Chrome, IE, Firefox - all ok, but Safari, nope…
AlexC
  • 109
  • 1
  • 10
0
votes
1 answer

Java: Prepare for forced shutdown

I am making an application in Java where I want to always have the data saved to a file with minimal saving operations. In other words, whenever the app is closed or an exception is thrown, it will first attempt to save. The one problem: the POWER…
hyper-neutrino
  • 5,272
  • 2
  • 29
  • 50
0
votes
2 answers

How to make a Java program that cannot be killed from command line?

I am trying to run a Java program that appears to have "9 lives". It simply prints out, in an infinite loop, the following when we run it: GlobalVar = 1 GlobalVar = 1 GlobalVar = 1 /* etc */ GlobalVar = 1 GlobalVar = 1 Then once we CTRL+C and kill…
Caffeinated
  • 11,982
  • 40
  • 122
  • 216
0
votes
1 answer

Execute method at shutdown throws log4j illegal state exception unable to register shutdown

I'd like to close connections when exiting the swing application. So as suggested here: Running a method when closing the program? I added following code, in the main to the main thread, ending up with following stack trace. It does perform my…
Finn
  • 1
  • 3
0
votes
1 answer

script to see the changes done by a developer while commiting a file in svn

I am looking for a script such that whenever any developer do any changes in file than email should be sent to the whole group. For that I have added the DL that is in post shutdown hook script, but my concern is that mail body should…
ndsfd ddsfd
  • 235
  • 1
  • 5
  • 13
0
votes
3 answers

How to call shutdown hook from storm topology main class?

i have a storm topology class which starts a kafka spout and bolts. This class is main class. I am trying to clean exit storm topology, so i have created a shutdown hook in side that topology main method. //Shutdown hook …
Swapnil1988
  • 269
  • 3
  • 6
  • 18
0
votes
1 answer

Calling shutDownHook for certain exit codes?

As far as I know System.exit triggers shutdown hook(if any hook registered) so while shutdown hook thread is waiting for any other thread to join if other thread calls System.exit() I think a deadlock occurs so is there any solution to register…
trueLife
  • 103
  • 7
0
votes
1 answer

Why doesn't Java's .addShutdownHook work for Ctrl-C on BeagleBone Black? Is there a workaround?

Problem I have run the following Clojure code on both my Ubuntu development machine and the BeagleBone Black, and can confirm that it works on the former but not the latter. (defn setup-shutdown-hook! [f] …
metasoarous
  • 2,854
  • 1
  • 23
  • 24
0
votes
1 answer

Raspberry Pi Java Shutdown hook

I have a Raspberry Pi running Java 1.8.0 and a file called test.jar. When I run the code and then stop the program with Ctrl+Z the Shutdown hook does not run but when I run the code on windows and stop it, the shutdown hook will work. How can I fix…
oliverjrose99
  • 47
  • 1
  • 8
0
votes
1 answer

Java shutdown hook and writing to an Excel Sheet

So high level I have a program that reads data from an Excel SpreadSheet stores it in a list of hash maps, does dome operations, gets a status, and then writes the information back the Excel SpreadSheet. As of right now if an Exception of some sort…
cmeinert
  • 11
  • 4
0
votes
1 answer

GUI application shuts down before the Shutdown Hook message appears

Java newbie here. Here's the deal: I've got the following, simple shutdown-hook: class SHook implements Runnable { public void run() { System.out.println("SH is done."); } } And I've got a GUI application with a TextArea were the output is…
0
votes
0 answers

Make a Java application prevent more than one instance of itself

I see various posts on this subject but no conclusive answers except for a C# way. Using a lock file would work but is not pretty. I saw reference to using a shutdownhook, but without any elaboration. After combing the API doc of the Runtime class,…
casgage
  • 529
  • 1
  • 7
  • 18