Package org.opends.server.api
Class DirectoryThread
java.lang.Object
org.opends.server.api.DirectoryThread
- All Implemented Interfaces:
AlertGenerator
- 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.
DirectoryThread previously extended Thread
. It cannot be done anymore as extending Thread
is
not supported by the test frameworks based on the simulator. As such, DirectoryThread
is now a wrapper
around a Thread
. It is still possible to get the DirectoryThread
instance associated to the
current thread by using currentDirectoryThread()
.
-
Constructor Summary
ConstructorsModifierConstructorDescriptionDirectoryThread
(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.static DirectoryThread
Returns the current DirectoryThread instance associated with the current thread, or null if there is none.Retrieves information about the set of alerts that this generator may produce.Retrieves the task with which this thread is associated.Returns the fully-qualified name of the Java class for this alert generator implementation.Returns the DN of the configuration entry with which this alert generator is associated.getName()
Returns the name of this thread.protected ServerContext
Returns the server context.Returns The stacktrace of this thread.void
Instructs the current thread to initiate the shutdown process.void
Interrupts this thread, which will cause it to stop processing and return from its run method.final boolean
isAlive()
Returns whether this thread is alive.boolean
isDaemon()
Returns The daemon status of this thread.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.void
join()
Wait for this thread to finish.void
join
(long millis) Wait for this thread to finish.void
run()
protected void
setAssociatedTask
(Task task) Sets the task with which this thread is associated.void
setDaemon
(boolean daemon) Set the daemon state of this thread.void
Set the name of this thread.void
start()
Start this thread.final Thread
thread()
Returns theThread
associated with this DirectoryThread.void
wakeup()
Wake up the thread if it is waiting.
-
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
-
currentDirectoryThread
Returns the current DirectoryThread instance associated with the current thread, or null if there is none.- Returns:
- the current DirectoryThread instance associated with the current thread, or null if there is none
-
getName
Returns the name of this thread.- Returns:
- the name of this thread
-
getStackTrace
Returns The stacktrace of this thread.- Returns:
- The stacktrace of this thread
-
isDaemon
public boolean isDaemon()Returns The daemon status of this thread.- Returns:
- The daemon status of this thread
-
join
Wait for this thread to finish.- Parameters:
millis
- the maximum amount of time in milliseconds to wait for this thread to finish.- Throws:
InterruptedException
- if the current thread is interrupted while waiting.
-
join
Wait for this thread to finish.- Throws:
InterruptedException
- if the current thread is interrupted while waiting.
-
setDaemon
public void setDaemon(boolean daemon) Set the daemon state of this thread.- Parameters:
daemon
- the daemon state of this thread
-
setName
Set the name of this thread.- Parameters:
name
- the new name of this thread
-
start
public void start()Start this thread. -
thread
Returns theThread
associated with this DirectoryThread.- Returns:
- the
Thread
associated with this DirectoryThread
-
run
public void run()If this thread was constructed using a separateRunnable
run object, then thatRunnable
object 's run method is called; otherwise, this method does nothing and returns. Subclasses ofDirectoryThread
should override this method. -
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.
-
isAlive
public final boolean isAlive()Returns whether this thread is alive. A thread is alive if it has been started and has not yet terminated- Returns:
- whether this thread is alive
-
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()Interrupts this thread, which will cause it to stop processing and return from its run method.This method will also initiate the shutdown process by calling
initiateShutdown()
. -
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.
-
getComponentEntryDn
Description copied from interface:AlertGenerator
Returns the DN of the configuration entry with which this alert generator is associated.- Specified by:
getComponentEntryDn
in interfaceAlertGenerator
- Returns:
- the DN of the configuration entry with which this alert generator is associated
-
getClassName
Description copied from interface:AlertGenerator
Returns the fully-qualified name of the Java class for this alert generator implementation.- Specified by:
getClassName
in interfaceAlertGenerator
- Returns:
- the fully-qualified name of the Java class for this alert generator implementation
-
getAlerts
Description copied from interface:AlertGenerator
Retrieves information about the set of alerts that this generator may produce. The map returned should be between the notification type for a particular notification and the human-readable description for that notification. This alert generator must not generate any alerts with types that are not contained in this list.- Specified by:
getAlerts
in interfaceAlertGenerator
- Returns:
- Information about the set of alerts that this generator may produce.
-