Package org.apache.sis.internal.system
Class DaemonThread
java.lang.Object
java.lang.Thread
org.apache.sis.internal.system.DaemonThread
- All Implemented Interfaces:
Runnable
- Direct Known Subclasses:
DelayedExecutor
,ReferenceQueueConsumer
Base class for all daemon threads in the SIS library. All
DaemonThread
instances are
expected to run for the whole JVM lifetime (they are not executor threads).
This class provides a isKillRequested()
flag which shall be tested by the subclasses.
It is okay to test this flag only when catching InterruptedException
, as below:
- Since:
- 0.3
- Version:
- 0.3
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
Set totrue
when a kill is requested.private final DaemonThread
The previous element in a chain ofDaemonThread
s.Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
DaemonThread
(String name, DaemonThread lastCreatedDaemon) Creates a new daemon thread. -
Method Summary
Modifier and TypeMethodDescriptionprotected final boolean
Returnstrue
if this daemon thread shall terminate.protected boolean
Returnstrue
if this thread seems to be blocked for a time long enough for suspecting a problem.(package private) static void
killAll
(DaemonThread thread, long stopWaitingAt) Sends a kill signal to all threads in the chain starting by the given thread, and waits for the threads to die before to return.listStalledThreads
(DaemonThread thread) Returns the list of stalled or dead threads, ornull
if none.abstract void
run()
Must be overridden by subclass for performing the actual work.Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, toString, yield
-
Field Details
-
previous
The previous element in a chain ofDaemonThread
s. We maintain a linked list ofDaemonThread
to be killed whenkillAll(DaemonThread, long)
will be invoked. We do not rely on the thread listed by theThreads.DAEMONS
group because in an OSGi context, we need to handle separately the threads created by each SIS module. -
killRequested
private volatile boolean killRequestedSet totrue
when a kill is requested.
-
-
Constructor Details
-
DaemonThread
Creates a new daemon thread. This constructor sets the daemon flag totrue
. The thread will be allocated a small stack size on the assumption that it will execute only small and quick methods, without deep nesting levels.We need to maintain a list of daemon threads created by each SIS module in order to kill them at shutdown time (not strictly necessary for pure JSEE applications, but required in OSGi environment). Each module using
SeeDaemonThread
shall maintain its own list (don't use the list of another module), like below:ReferenceQueueConsumer
for a real example.- Parameters:
name
- the thread name.lastCreatedDaemon
- the previous element in a chain ofDaemonThread
s, ornull
. Each SIS module shall maintain its own chain, if any.
-
-
Method Details
-
run
public abstract void run()Must be overridden by subclass for performing the actual work. -
isStalled
protected boolean isStalled()Returnstrue
if this thread seems to be blocked for a time long enough for suspecting a problem. The default implementation always returnsfalse
. Subclasses are encouraged to provide some problem detection mechanism here if they can. For example if the head of a queue seems to be never removed, then maybe the process consuming that queue is blocked.- Returns:
true
if this thread seems to be stalled.
-
isKillRequested
protected final boolean isKillRequested()Returnstrue
if this daemon thread shall terminate. This happen at shutdown time.- Returns:
true
if this daemon thread shall terminate.
-
killAll
Sends a kill signal to all threads in the chain starting by the given thread, and waits for the threads to die before to return.This method is for internal use by Apache SIS shutdown hooks only. Users should never invoke this method explicitly.
- Parameters:
thread
- the first thread in the chain of threads to kill.stopWaitingAt
- aSystem.nanoTime()
value telling when to stop waiting. This is used for preventing shutdown process to block an indefinite amount of time.- Throws:
InterruptedException
- if another thread invokedThread.interrupt()
while we were waiting for the daemon threads to die.- See Also:
-
listStalledThreads
Returns the list of stalled or dead threads, ornull
if none. The returned list should always be null. A non-empty list would be a symptom for a severe problem, probably requiring an application reboot.This method is for internal use by Apache SIS only. Users should never invoke this method explicitly.
- Parameters:
thread
- the first thread in the chain of threads to verify.- Returns:
- the list of stalled or dead threads, or
null
if none.
-