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

what event is fired when you close a javafx application from the task bar and how do you handle it?

I've seen a few posts about this that have answers marked as correct even though they don't seem to work for which could be for a number of reasons. What I would like is to handle the event in which the user closes the application from the taskbar…
user12252748
2
votes
1 answer

Springboot shutdown hook not working with Windows TaskKill

I have an Apache Camel Spring Boot Java 8 app that runs on Windows 10. It shuts down gracefully when CTRL-C is pressed, although sometimes I have to press it twice. But when using TaskKill, the answer always is C:\Windows\system32>taskkill /PID…
Josi
  • 306
  • 2
  • 14
2
votes
0 answers

What happens when shutdown hooks or finalizers don’t complete?

Java Concurrency in Practice says: In an orderly shutdown, the JVM first starts all registered shutdown hooks. Shutdown hooks are unstarted threads that are registered with Runtime.addShutdownHook. The JVM makes no guarantees on the order in which…
Jason Law
  • 965
  • 1
  • 9
  • 21
2
votes
0 answers

delphi windowless shutdown detect

I'm writing a very simple Delphi 2007 program that monitors system activities. The program just hooks a DLL procedure and has to simply wait until the system shutsdown since all the handling is done in the DLL callback. Is a windowless program since…
alvaroc
  • 433
  • 5
  • 14
2
votes
1 answer

Kafka Streams Shutdown Hook and Unexpected Exception Handling in the same Stream application

I was tasked with tearing down a Dev environment and setting it up again from scrap to verify our CI-CD processes; only problem was that I messed up creating one topic and so the Kafka Streams application exited with an error. I dug into it and…
2
votes
0 answers

Detect shutdown/reboot in OSX from C++ app

How can I detect a shutdown/reboot on OSX from a C++ application? I need to report that the system is gonna shutdown/reboot to a central server sending a packet so it should be fast enough so network interfaces don't go down before the packet is…
user1618465
  • 1,813
  • 2
  • 32
  • 58
2
votes
3 answers

Detect windows logout event in Java application

Is there a library that I can use with Java to listen for user logout and possibly other Windows events? (Even better if it supports multiple platforms!) I remember reading about a library of this sort a number of years ago, but can't seem to find…
Chris Mazzola
  • 5,807
  • 5
  • 22
  • 15
2
votes
3 answers

How to implement thread pool that will automatically shutdown at end of execution?

I'm writing a Java client which could theoretically be used in a different environment: Java main(), in a servlet container, or via dependency injection. The client implements internal connection thread pooling. The problem with this approach is…
Maxim Veksler
  • 29,272
  • 38
  • 131
  • 151
2
votes
2 answers

Finding out who calls jvm shutdown hook

I have Windows Desktop application based on Java. I have kept defaultuncaughtexceptionahandler and shutdown hook in my application. I have some user called exit points like , user clicks on exit, some error conditions etc. All the user exit points…
saurav
  • 5,388
  • 10
  • 56
  • 101
2
votes
2 answers

Why Log4j2's RollingFile appender prevents a stand alone application to terminate for 60 sec?

This code reproduces what I think is a bug of Log4j2. It's a simple loop that logs 2000 messages with two appenders: a console appender and a rolling file appender that rolls the file every 5Kb. This limit is intentionally low to reproduce what I…
danidemi
  • 4,404
  • 4
  • 34
  • 40
2
votes
1 answer

Stop Hook Is Not Being Called When Play Application Is Shutting Down

I am developing an application using Play Framework 2.5.5 and I'm having issues with adding a stop hook to my class. I have a class called Broom like following to schedule a cleaning job. @ImplementedBy(classOf[Broom]) trait…
2
votes
0 answers

How to shutdown thread(Quartz Scheduler, ActiveMQ, logback, MongoDB) gracefully in Spring Boot on tomcat

We have a spring-boot based web application and using these components below jmsTemplate for ActiveMQ connection Spring quartz scheduler for scheduling task spring-boot-starter-data-mongodb and mongo-java-driver as persistence Logback as…
Bruce
  • 647
  • 2
  • 12
  • 30
2
votes
2 answers

Java do complete units of work until signaled to stop

I have some (Linux) C code which I am converting to Java. The code has a main loop that checks for a TERM signal from the OS at each looptop and blocks signals otherwise. This is so each "unit of work" it does in the loop is done completely (not…
John Hascall
  • 9,176
  • 6
  • 48
  • 72
2
votes
0 answers

Spark streaming fails to launch worker on worker failure

I'm trying to setup a spark cluster and I've come across an annoying bug... When I submit a spark application it runs fine on workers until I kill one (for example by using stop-slave.sh on the worker node). When the worker is killed spark will then…
2
votes
1 answer

How is CTRL+C handled in jline2

I had a hard time trying to understand how is CTRL+C handled in jline2. I found in consoleReader.readline an exception will be thrown if handleUserInterrupt is set to true. But I wonder before this exception is thrown, how is CTRL+C is trapped and…
pythonician_plus_plus
  • 1,244
  • 3
  • 15
  • 38