Package org.opends.server.api
Class DirectoryThread
java.lang.Object
java.lang.Thread
org.opends.server.api.DirectoryThread
- All Implemented Interfaces:
Runnable
- Direct Known Subclasses:
ChangeNumberIndexer
,DirectoryServerShutdownHook
,IdleTimeLimitThread
,MonitoringPublisher
,TaskScheduler
This class defines a generic thread that should be the superclass for all threads created by the Directory Server.
That is, instead of having a class that "extends Thread", you should make it "extends DirectoryThread". This provides
various value-added capabilities, including:
- It helps make sure that all threads have a human-readable name so they are easier to identify in stack traces.
- It can capture a stack trace from the time that this thread was created that could be useful for debugging purposes.
- It plays an important role in ensuring that log messages generated as part of the processing for Directory Server tasks are properly captured and made available as part of that task.
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
-
Field Summary
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
-
Constructor Summary
ModifierConstructorDescriptionDirectoryThread
(Runnable target, String threadName, ServerContext serverContext) Creates a new instance of this directory thread with the specified name and with the specified target as its run object.protected
DirectoryThread
(String threadName, ServerContext serverContext) Creates a new instance of this directory thread with the specified name. -
Method Summary
Modifier and TypeMethodDescriptionprotected boolean
checkForShutdownOrWait
(long waitTimeMs) Checks for thread shutdown, or waits for the specified time.protected boolean
checkForShutdownOrWait
(long waitTimeMs, BooleanSupplier canSleep) Checks for thread shutdown, or waits for the specified time.Retrieves the task with which this thread is associated.protected ServerContext
Returns the server context.void
Instructs the current thread to initiate the shutdown process.void
boolean
Returns whether the shutdown process has been initiated on the current thread.boolean
Returnstrue
if this thread has been started.boolean
Returnstrue
if this thread is waiting to be started.protected void
setAssociatedTask
(Task task) Sets the task with which this thread is associated.void
wakeup()
Wake up the thread if it is waiting.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, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, run, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, toString, yield
-
Constructor Details
-
DirectoryThread
Creates a new instance of this directory thread with the specified name.- Parameters:
threadName
- The human-readable name to use for this thread for debugging purposes.serverContext
- The server context.
-
DirectoryThread
Creates a new instance of this directory thread with the specified name and with the specified target as its run object.- Parameters:
target
- The target runnable object.threadName
- The human-readable name to use for this thread for debugging purposes.serverContext
- The server context.
-
-
Method Details
-
getServerContext
Returns the server context.- Returns:
- the server context
-
getAssociatedTask
Retrieves the task with which this thread is associated. This will only be available for threads that are used in the process of running a task.- Returns:
- The task with which this thread is associated, or
null
if there is none.
-
setAssociatedTask
Sets the task with which this thread is associated. It may benull
to indicate that it is not associated with any task.- Parameters:
task
- The task with which this thread is associated.
-
isStarting
public boolean isStarting()Returnstrue
if this thread is waiting to be started.- Returns:
true
if this thread is waiting to be started.
-
isStarted
public boolean isStarted()Returnstrue
if this thread has been started.- Returns:
true
if this thread has been started.
-
isShutdownInitiated
public boolean isShutdownInitiated()Returns whether the shutdown process has been initiated on the current thread. It also returns true when the thread is actually terminated.Waiting for the thread to terminate should be done by invoking one of the
Thread.join()
methods.- Returns:
- true if the shutdown process has been initiated on the current thread, false otherwise.
-
interrupt
public void interrupt() -
initiateShutdown
public void initiateShutdown()Instructs the current thread to initiate the shutdown process. The actual shutdown of the thread is a best effort and is dependent on the implementation of theThread.run()
method. -
wakeup
public void wakeup()Wake up the thread if it is waiting. -
checkForShutdownOrWait
protected boolean checkForShutdownOrWait(long waitTimeMs) Checks for thread shutdown, or waits for the specified time. To be called from a Thread's run() method in a loop.Typical usage pattern is:
while (!isShutdownInitiated()) { // ...business logic goes here... if (checkForShutdownOrWait(waitTimeMs)) { return; } }
- Parameters:
waitTimeMs
- the wait time in milliseconds- Returns:
true
if the thread can continue running,false
if shutdown was initiated for this thread and it must stop.
-
checkForShutdownOrWait
Checks for thread shutdown, or waits for the specified time. To be called from a Thread's run() method in a loop.Typical usage pattern is:
while (!isShutdownInitiated()) { // ...business logic goes here... if (checkForShutdownOrWait(waitTimeMs, canSleepSupplier)) { return; } }
- Parameters:
waitTimeMs
- the wait time in millisecondscanSleep
- whether the- Returns:
true
if the thread can continue running,false
if shutdown was initiated for this thread and it must stop.
-