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
6
votes
4 answers

How to ensure a piece of code is run before exiting a java application

I'm using a licensed API which has a method to acquire/release a license object from a license server that has a finite number of licenses. At the beginning of my application, I call the method to acquire the license, but I want to make sure that…
fo_x86
  • 2,583
  • 1
  • 30
  • 41
5
votes
1 answer

How to add a shutdown hook to a Rust program?

I'm writing a Rust program which needs to save some data at the end of its execution, whatever happens. In the Java world, I would do that with a shutdown hook. There is a crate, aptly called shutdown_hooks, but it seems only able to register extern…
Riduidel
  • 22,052
  • 14
  • 85
  • 185
5
votes
1 answer

In PHP does max_execution_time affect shutdown functions that are run through a register_shutdown_function() call?

I have a shutdown function registered using register_shutdown_function() that takes a long time to complete. Will PHP's max_execution_time cause PHP to terminate this function or will it run until it completes or errors out?
Joe Lencioni
  • 10,231
  • 18
  • 55
  • 66
5
votes
0 answers

Configure Linux to Suspend to Disk on ACPI G2 Soft Off -- so Google Compute Engine can suspend and restore preemptible machine via disk

Google Compute Engine rents all size Linux VMs from 1 core to 64 cores at various prices. There are "preempt-able" instances for about 1/4 the price of guaranteed instances, but the the preempt-able instances can be terminated at any time (with an…
xmedeko
  • 7,336
  • 6
  • 55
  • 85
5
votes
7 answers

jvm: is it possible to find out process is shutting down due to an OOM while in the shutdown hook?

I have a critical process running in java (1.6), with a registered shutdown hook. In some instance where I get a OOM issue (more details below about the issue), the process stops suddenly, I don't get any of my logs, my catch(Throable x) is not…
Persimmonium
  • 15,593
  • 11
  • 47
  • 78
5
votes
2 answers

Handle if a process killed externally

I am writing a program where i am creating multiple threads in a process. I need to handle that if the process is killed externally by someone by using kill -9 signal or Ctrl + C, my program should do some action before closing e.g. it should change…
chumak
  • 117
  • 1
  • 9
5
votes
2 answers

Proper shutdown of JVM when launching from C++

I'm launching JVM from C++ code via JNI. I have a problem that when just quitting my C++ process it seems some shutdown hooks from JVM are not run, and therefore some temp resources are still being around, that in my particular case prevents…
Vyacheslav
  • 1,186
  • 2
  • 15
  • 29
5
votes
1 answer

Could not load org.apache.hadoop.util.ShutdownHookManager when shutdown tomcat server

I have a simple web application run on tomcat. This web application read and write file to HDFS. The issue I am facing is each time when I stop the server by using ./bin/shutdown.sh. I am getting could not load hadoop shutdownHookManager…
Terminal User
  • 873
  • 3
  • 13
  • 21
5
votes
1 answer

Why won't this shutdownhook work?

This is my main method and it contains a shutdownhook: public static void main(String args[]) { Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { JOptionPane.showMessageDialog(null,…
Igor
  • 1,532
  • 4
  • 23
  • 44
5
votes
2 answers

What happens when an exception is thrown inside a shutdown hook in java

If an uncaught exception is thrown during the execution of a shutdown hook in java, does the jvm exit immediately without running the rest of the registered shutdown hooks (if any)? From the javadocs: Uncaught exceptions are handled in shutdown…
fo_x86
  • 2,583
  • 1
  • 30
  • 41
5
votes
3 answers

Shutdown hook from UNIX

I am trying to get my Java program to exit gracefully on my unix server. I have a jar file, which I start through a cron job in the morning. Then in the evening, when I want to shut it down, I have a cron job which calls a script that finds the…
Reese
  • 173
  • 3
  • 7
4
votes
2 answers

How long does the JVM allow shutdown hooks to run before calling halt?

Follow up from previous question If you've read the previous question an alternative title may be: How often should my daemon check to see if it's been interrupted?
Aaron J Lang
  • 2,048
  • 3
  • 20
  • 27
4
votes
2 answers

Find, from a ShutdownHook, why a program exits

If I've got a Java program that can exit for various reasons, like: because the main window, which is set to "exit on close", was closed because there are some System.exit( 0 ) in the code because there are no more window at all (and none was set…
Cedric Martin
  • 5,945
  • 4
  • 34
  • 66
4
votes
1 answer

Any Shutdown Hook when application is "Force Closed"?

Is there any way to make the program go through the shutdown hook if the user forces java to close (through the task manager or by closing the corresponding batch file). My program currently runs and executes well, if the user closes the GUI then it…
Cody
  • 870
  • 4
  • 21
  • 38
4
votes
2 answers

Why does ShutdownHookThread 'setDaemon true'

I recently needed to add a shutdown hook to a Scala app I have, and I discovered that Scala provides a helper for this called ShutdownHookThread. In its source I noticed that it sets the new thread to be a daemon thread. def apply(body: => Unit):…
overthink
  • 23,985
  • 4
  • 69
  • 69
1 2
3
14 15