public final class ShutdownHook extends Thread
shutdown hook,
returning a CountDownLatch on
which the main thread waits. This latch is decremented when
the Java Runtime executes the shutdown hook. Since the
latch is size one, CountDownLatch.await() will return.
This returned latch may be passed to other objects which can decrement the latch and so signal that the application is to terminate.
An example Java main method uses this class as follows:
import java.util.concurrent.CountDownLatch;
public static void main(final String args[])
{
// 1. Check the command line arguments.
// 2. Initialize the application.
// 3. Set up the shutdown hook.
final CountDownLatch signal = ShutdownHook.addShutdownHook();
// 4. Start application thread.
// 5. Wait for the JVM to stop.
try {
signal.await();
} catch (InterruptedException interrupt) {}
// 6. Tear down the application.
}
Thread.State, Thread.UncaughtExceptionHandlerMAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY| Modifier and Type | Method and Description |
|---|---|
static CountDownLatch |
addShutdownHook()
Returns the signal used by the shutdown hook to notify
the caller when the JVM is shutdown.
|
static void |
removeShutdownHook()
Unhooks the shutdown hook instance from the system
runtime.
|
void |
run()
Decrements the signal which results in a return from
CountDownLatch.await(). |
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yieldpublic void run()
CountDownLatch.await().public static CountDownLatch addShutdownHook()
CountDownLatch.await() returns
as soon as this latch is decremented.public static void removeShutdownHook()
Copyright © 2019. All rights reserved.