org.apache.excalibur.thread.impl
Class WorkerThread

java.lang.Object
  extended by java.lang.Thread
      extended by org.apache.excalibur.thread.impl.WorkerThread
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
SimpleWorkerThread

public class WorkerThread
extends java.lang.Thread

This class extends the Thread class to add recyclable functionalities.

Author:
Avalon Development Team

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
private static boolean ENABLE_DEBUG
          Enables debug output of major events.
private static boolean ENABLE_DETAIL_DEBUG
          Enables debug output of minor events.
private  boolean m_alive
          True if this thread is alive and not scheduled for shutdown.
private  boolean m_clearInterruptFlag
          True if this thread needs to clear the interrupt flag
private  AbstractThreadPool m_pool
          The thread pool this thread is associated with.
private  DefaultThreadControl m_threadControl
          The thread control associated with current work.
private  org.apache.excalibur.thread.Executable m_work
          The work currentlyy associated with worker (May be null).
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
protected WorkerThread(AbstractThreadPool pool, java.lang.ThreadGroup group, java.lang.String name)
          Allocates a new Worker object.
 
Method Summary
 void clearInterruptFlag()
          Clears the interrupt flag for this thread.
protected  void debug(java.lang.String message)
          Used to log major events against the worker.
protected  void debug(java.lang.String message, java.lang.Throwable throwable)
          Used to log major events against the worker.
protected  void detailDebug(java.lang.String message)
          Used to log minor events against the worker.
protected  void detailDebug(java.lang.String message, java.lang.Throwable throwable)
          Used to log minor events against the worker.
 void dispose()
          Set the alive variable to false causing the worker to die.
protected  org.apache.excalibur.thread.ThreadControl execute(org.apache.excalibur.thread.Executable work)
          Set the Work code this Worker must execute and notifies its thread to do it.
protected  void executeAndWait(org.apache.excalibur.thread.Executable work)
          Set the Work code this Worker must execute and notifies its thread to do it.
protected  void postExecute()
          Overide this method to execute something after each bit of "work".
protected  void preExecute()
          Overide this method to execute something before each bit of "work".
protected  void recycleThread()
          Implement this method to replace thread back into pool.
 void run()
          The main execution loop.
private  void waitForWork()
          For for new work to arrive or for the thread to be destroyed.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, 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, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ENABLE_DEBUG

private static final boolean ENABLE_DEBUG
Enables debug output of major events. Subclasses which implement their own logging do not require this to be true.

See Also:
Constant Field Values

ENABLE_DETAIL_DEBUG

private static final boolean ENABLE_DETAIL_DEBUG
Enables debug output of minor events. Subclasses which implement their own logging do not require this to be true.

See Also:
Constant Field Values

m_work

private org.apache.excalibur.thread.Executable m_work
The work currentlyy associated with worker (May be null).


m_threadControl

private DefaultThreadControl m_threadControl
The thread control associated with current work. Should be null if work is null.


m_alive

private boolean m_alive
True if this thread is alive and not scheduled for shutdown.


m_clearInterruptFlag

private boolean m_clearInterruptFlag
True if this thread needs to clear the interrupt flag


m_pool

private final AbstractThreadPool m_pool
The thread pool this thread is associated with.

Constructor Detail

WorkerThread

protected WorkerThread(AbstractThreadPool pool,
                       java.lang.ThreadGroup group,
                       java.lang.String name)
Allocates a new Worker object.

Method Detail

run

public final void run()
The main execution loop.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

recycleThread

protected void recycleThread()
Implement this method to replace thread back into pool.


postExecute

protected void postExecute()
Overide this method to execute something after each bit of "work".


preExecute

protected void preExecute()
Overide this method to execute something before each bit of "work".


clearInterruptFlag

public void clearInterruptFlag()
Clears the interrupt flag for this thread. Since Java does not provide a method that does this for an external thread, we have to verify that we are in the WorkerThread. If the code calling this method does not originate from this thread, we set a flag and wait for it to be called internally.


dispose

public void dispose()
Set the alive variable to false causing the worker to die. If the worker is stalled and a timeout generated this call, this method does not change the state of the worker (that must be destroyed in other ways).

This is called by the pool when it is removed.


execute

protected org.apache.excalibur.thread.ThreadControl execute(org.apache.excalibur.thread.Executable work)
Set the Work code this Worker must execute and notifies its thread to do it.


executeAndWait

protected void executeAndWait(org.apache.excalibur.thread.Executable work)
Set the Work code this Worker must execute and notifies its thread to do it. Wait until the executable has finished before returning.


waitForWork

private void waitForWork()
For for new work to arrive or for the thread to be destroyed.


debug

protected void debug(java.lang.String message)
Used to log major events against the worker. Creation, deletion, uncaught exceptions etc.

This implementation is a Noop. Subclasses can override to actually do some logging.

Parameters:
message - Message to log.

debug

protected void debug(java.lang.String message,
                     java.lang.Throwable throwable)
Used to log major events against the worker. Creation, deletion, uncaught exceptions etc.

This implementation is a Noop. Subclasses can override to actually do some logging.

Parameters:
message - Message to log.
throwable - Throwable to log with the message.

detailDebug

protected void detailDebug(java.lang.String message)
Used to log minor events against the worker. Start and stop of individual pieces of work etc. Separated from the major events so that they are not lost in a sea of minor events.

This implementation is a Noop. Subclasses can override to actually do some logging.

Parameters:
message - Message to log.

detailDebug

protected void detailDebug(java.lang.String message,
                           java.lang.Throwable throwable)
Used to log minor events against the worker. Start and stop of individual pieces of work etc. Separated from the major events so that they are not lost in a sea of minor events.

This implementation is a Noop. Subclasses can override to actually do some logging.

Parameters:
message - Message to log.
throwable - Throwable to log with the message.