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

How netbeans stops a run?

I'm programming with Java in Linux using Netbeans 7 and as my program (sometimes) could not exit (not in this eon, maybe) I create a thread to handle shutdown Runtime.getRuntime().addShutdownHook(new StopThread()); But when I launch the code with…
Fabio Filippi
  • 1,732
  • 25
  • 40
4
votes
3 answers

How to do graceful shutdown/termination of java processes?

I am running some java apps and I need to shutdown/close all apps gracefully from windows bat script. So my question is: How to invoke shutdown hook by windows bat script and gracefully shutdown java program. Any suggestion is appreciated. Thanks…
SmartSolution
  • 2,320
  • 5
  • 37
  • 49
4
votes
3 answers

Controlled application shut-down strategy

Our (Windows native C++) app is composed of threaded objects and managers. It is pretty well written, with a design that sees Manager objects controlling the lifecycle of their minions. Various objects dispatch and receive events; some events come…
dripfeed
  • 387
  • 1
  • 10
4
votes
1 answer

Java ShutdownHook Unable to Join Main Thread when Run From JNI

I have some Java code to create a shutdown hook in order to exit cleanly when the client presses ctrl+C: private static void shutdownHandler(Thread mainThread) { try { mainThread.join(30000); } catch (InterruptedException e) { …
Jeff G
  • 4,470
  • 2
  • 41
  • 76
4
votes
2 answers

Spring boot application shutting down unexpectedly and log issues

I have a web app developed with spring boot 1.5.6.RELEASE. The application is currently on production serving user base of ~3000 users while running on two servers. My issue is when I shutdown applications for a new release and start new version…
4
votes
2 answers

Why does a java.util.Logger in a ShutdownHook within JBoss 5.1 not always prints to server.log?

I have a EJB3-Timer within a JBoss 5.1. edit: The ShutdownHook should set a flag, that the doTimeOut() can terminate graceful (otherwise it would complete the job, and the shutdown is stopped until this timer has finished). @Stateless class Timer…
Christian Kuetbach
  • 15,850
  • 5
  • 43
  • 79
4
votes
1 answer

Ant build shutdown - Ctrl C

I have a set of ant tasks that I use to run my test suite, occasionally one of those tests will freeze and my entire test suite will hang. I added a shutdown handler so when I hit Ctrl+C ant will shutdown gracefully and give me a report with the…
Michael
  • 3,498
  • 5
  • 27
  • 32
4
votes
2 answers

Differentiate shutdowns more than Runtime.setShutdownHook() in Java

Is there a way to add a shutdown hook that is only fired when the program is shut down because of a System shutdown. I don't want the code to run when the code finishes regularly, only on other Exits. With addShutDownHook the Thread runs no matter…
lazyguy
  • 107
  • 6
4
votes
4 answers

Java Threads and Shutdown Hook

I've just run into an interesting issue. It seems that if, in Java, a thread calls System.exit() it cannot then be joined via Thread.join(). This is causing me issues as I want to use a shutdown hook to clean up after my application, such…
greydamian
  • 153
  • 2
  • 9
4
votes
1 answer

How to gracefully stop Guava AbstractScheduledService using a shutdown hook?

I am using an AbstractScheduledService with a scheduler. A simple pattern like: class MyService extends AbstractScheduledService { // KEEP THIS VAR IN MIND W.R.T the SHUTDOWN_HOOK BELOW public static volatile boolean keepRunning = true; …
Urjit
  • 1,150
  • 8
  • 15
4
votes
3 answers

interrupt all threads in Java in shutdown hook

I have a simple java program that creates a series of temporary files stored in a local tmp directory. I have added a simple shutdown hook that walks through all files and deletes them, then deletes the tmp directory, before exiting the program. …
ewok
  • 20,148
  • 51
  • 149
  • 254
4
votes
1 answer

How to hook NTShutdownSystem in C# in order to differentiate between shutdown and reboot

unfortunatly, when you listen to WM_QUERYENDSESSION, you do not get the information if the user has requested a reboot or a shutdown. This is really bad design, but it's the way Windows is, so I was thinking of hooking the call to NTShutdownSystem,…
Erik
  • 2,316
  • 9
  • 36
  • 58
3
votes
2 answers

Running Code on page leave [Actionscript]

How can I run code in Actionscript 3 when a page with my flash object is... well... I'm not sure of the term. When the webpage is left and the flash applet is exited?
James T
  • 3,292
  • 8
  • 40
  • 70
3
votes
3 answers

Detecting that JVM is shutting down

I have a Swing application that handles Ctrl+C using addShutdownHook(), and it works fine until one of the shutdown tasks I have calls a function that under normal circumstances changes a JLabel text, at which point it hangs. I assume the problem is…
Jason S
  • 184,598
  • 164
  • 608
  • 970
3
votes
1 answer

Spring Boot: gracefully shutdown by controlling termination order involving MongoClient

I have a Spring Boot application that spawns many threads using an AsyncTaskExecutor (the number is predefined) The threads execute an infinite loop, that reads from some queue and process objects, so I don't really have a rejection policy…
IsaacLevon
  • 2,260
  • 4
  • 41
  • 83