public final class ThreadDeathWatcher
extends java.lang.Object
This thread starts a daemon thread to check the state of the threads being watched and to invoke their
associated Runnable
s. When there is no thread to watch (i.e. all threads are dead), the daemon thread
will terminate itself, and a new daemon thread will be started again when a new watch is added.
Modifier and Type | Class and Description |
---|---|
private static class |
ThreadDeathWatcher.Entry |
private static class |
ThreadDeathWatcher.Watcher |
Modifier and Type | Field and Description |
---|---|
private static InternalLogger |
logger |
private static java.util.Queue<ThreadDeathWatcher.Entry> |
pendingEntries |
private static java.util.concurrent.atomic.AtomicBoolean |
started |
(package private) static java.util.concurrent.ThreadFactory |
threadFactory |
private static ThreadDeathWatcher.Watcher |
watcher |
private static java.lang.Thread |
watcherThread |
Modifier | Constructor and Description |
---|---|
private |
ThreadDeathWatcher() |
Modifier and Type | Method and Description |
---|---|
static boolean |
awaitInactivity(long timeout,
java.util.concurrent.TimeUnit unit)
Waits until the thread of this watcher has no threads to watch and terminates itself.
|
private static void |
schedule(java.lang.Thread thread,
java.lang.Runnable task,
boolean isWatch) |
static void |
unwatch(java.lang.Thread thread,
java.lang.Runnable task)
Cancels the task scheduled via
watch(Thread, Runnable) . |
static void |
watch(java.lang.Thread thread,
java.lang.Runnable task)
Schedules the specified
task to run when the specified thread dies. |
private static final InternalLogger logger
static final java.util.concurrent.ThreadFactory threadFactory
private static final java.util.Queue<ThreadDeathWatcher.Entry> pendingEntries
private static final ThreadDeathWatcher.Watcher watcher
private static final java.util.concurrent.atomic.AtomicBoolean started
private static volatile java.lang.Thread watcherThread
public static void watch(java.lang.Thread thread, java.lang.Runnable task)
task
to run when the specified thread
dies.thread
- the Thread
to watchtask
- the Runnable
to run when the thread
diesjava.lang.IllegalArgumentException
- if the specified thread
is not alivepublic static void unwatch(java.lang.Thread thread, java.lang.Runnable task)
watch(Thread, Runnable)
.private static void schedule(java.lang.Thread thread, java.lang.Runnable task, boolean isWatch)
public static boolean awaitInactivity(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
watch(Thread, Runnable)
,
this operation is only useful when you want to ensure that the watcher thread is terminated
after your application is shut down and there's no chance of calling
watch(Thread, Runnable)
afterwards.true
if and only if the watcher thread has been terminatedjava.lang.InterruptedException