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

Java shutdown hook not working on system shutdown

I am working on a JavaFX application that is to be run in background in most of time. So i need to execute code if a user close the application. But most of the time application will run in background/system tray so user may be forgot to close the…
-1
votes
1 answer

Java there is a way to guarantee a shutdown hook finish?

I need to perform some medium load on shutdown but in AWS the machine is giving only 2 or less seconds to the thread to perform and only 45-47% of load is consume. Is there is a way to increase this? or is nothing i can do! Thread…
chiperortiz
  • 4,751
  • 9
  • 45
  • 79
-1
votes
1 answer

UncaughtExceptionHandler and System.exit()

I've written a custom UncaughtExceptionHandler that should print the exception to the console and shut down the application with a custom exit code. The class looks like this: public class FatalUncaughtExceptionHandler implements…
Sirop
  • 57
  • 10
-1
votes
2 answers

Can't be able to store logout time on COMPUTER'S LOGOUT using Java Shutdown Hook

I am trying to store the time into a database when a user logs-off or logs-in into the computer but when I logs my computer off it does not store my current time into the database where if I press ctrl+c on console or terminate the program from…
asad sajjad
  • 85
  • 2
  • 12
-1
votes
2 answers

java thread stopped during OS initiated shutdown (contradicts JAVA docs!)

java.lang.Thread.setDaemon(boolean) The Java Virtual Machine exits when the only threads running are all daemon threads. YET IN THE BELOW CODE "SAFELY SHUTTING DOWN" never happens while interrupting(OS style) the process and no i'm not using…
-1
votes
1 answer

Receving a mail contains file name changed along with details in mail itself

I have one query is that I have a team of 10 developers which is use to commit the code at different intervals of time in SVN repository so in order to know what diifernt files have been commuted I have put the shutdown post hook in the script I…
user1633823
  • 347
  • 2
  • 5
  • 14
-2
votes
1 answer

Create shutdown hook and run it in the same thread in java

I know I can add a shutdown hook which will run in a new Thread, but how can I add a shutdown hook for the current thread, if even possible? I need some code to be run in the same thread as which created the shutdown hook but I couln't find an…
SimonH
  • 1,385
  • 15
  • 35
-2
votes
2 answers

what is tha actuall use of shut down hook in java

I m learning java, i m unable to understand the actual use of shutdown hook in java means where it actually use ? class MyThread extends Thread{ public void run(){ System.out.println("shut down hook task completed.."); } } …
pooja teotia
  • 97
  • 1
  • 1
  • 5
-2
votes
5 answers

Shutdownhook in Java

I want to write the system shutdown time to a txt file. I am using the shutdownhook thread. I have written the file writing mechanism in the run method of the thread. But it is not working.. This is my code.. public class JVMShutdownHookTest { …
Karthik
  • 53
  • 2
  • 3
  • 10
-2
votes
1 answer

Shutdown hook is not printing to console in JUnit Test executed via Maven

I have a simple JUnit test which is ran by Maven through a method marked by @Test annotation. I want to place the shutdown hook into that test because I want to print some test results if the user hits CTRL-C. I code the following…
KutaBeach
  • 1,445
  • 21
  • 43
1 2 3
14
15