com.ls.util.threading
Class ThreadPoolWorker

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--com.ls.util.threading.ThreadPoolWorker
All Implemented Interfaces:
Runnable

public class ThreadPoolWorker
extends Thread

Alternative implementation of a thread pool worker. Uses better synchronisation mechanisms to provide a secure and efficient contol over the worker thread.

Version:
$Revision: 1.6 $
Author:
Last modified by $Author: MFehrenbach $

Field Summary
static byte STATE_BUSY
          Indicates that the thread is processing a task
static byte STATE_IDLE
          Indicates, that the thread is running, but has nothing to do
static byte STATE_STOP
          Indicates, that the thread is stopped (not running).
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
ThreadPoolWorker(ThreadPool threadPool, String name)
          Constructs a new pool worker.
 
Method Summary
 void interrupt()
          Interrupts the currently executing task as well as the whole thread.
 void interruptTask()
          Interrupts the currently executing task.
 void joinTask()
          Waits until the currently executing task is finished.
 void joinTask(long timeout)
          Waits until the currently executing task is finished or the given timout period has passed (in the latter case a TimeoutException is raised).
 void run()
          The main loop of the worker thread.
 void runTask(Runnable task)
          Executes the given 'task' argument as soon as possible.
 void runTask(Runnable task, long timeout)
          Executes the given 'task' argument as soon as possible.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, 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

STATE_STOP

public static final byte STATE_STOP
Indicates, that the thread is stopped (not running).

STATE_IDLE

public static final byte STATE_IDLE
Indicates, that the thread is running, but has nothing to do

STATE_BUSY

public static final byte STATE_BUSY
Indicates that the thread is processing a task
Constructor Detail

ThreadPoolWorker

public ThreadPoolWorker(ThreadPool threadPool,
                        String name)
Constructs a new pool worker. The worker will be a child of the thread group that is represented by the 'threadPool' argument.

Parameters:
threadPool - The thread pool object to which this new worker thread belongs.
name - The name of the worker thread.
Method Detail

runTask

public void runTask(Runnable task)
             throws InterruptedException,
                    NotRunningException
Executes the given 'task' argument as soon as possible. Blocks until the thread is ready to process the given task. This method returns immeditately after the thread becomes ready. It will not block until the end of task execution.

Parameters:
task - The task that should be executed by this worker thread.
Throws:
InterruptedException - If the caller thread gets interrupted while waiting.
NotRunningException - If the thread is not running, either before start or after termination of the thread.

runTask

public void runTask(Runnable task,
                    long timeout)
             throws InterruptedException,
                    NotRunningException,
                    TimeoutException
Executes the given 'task' argument as soon as possible. Blocks until the thread is ready to process the given task or the given 'timeout' duration has passed. In the latter case a TimeoutException is raised.

This method returns immeditately after the thread becomes ready. It will not block until the end of task execution.

Parameters:
task - The task that should be executed by this worker thread.
timeout - The timeout period (in milliseconds).
Throws:
InterruptedException - If the caller thread gets interrupted while waiting.
NotRunningException - If the thread is not running, either before start or after termination of the thread.
TimeoutException - If the given timeout period passes and the thread is still not ready.

interruptTask

public void interruptTask()
Interrupts the currently executing task. If there is no executing task this method does nothing.


interrupt

public void interrupt()
Interrupts the currently executing task as well as the whole thread. If no task is currently executing or the thread has already terminated this method does nothing.

Overrides:
interrupt in class Thread

joinTask

public void joinTask()
              throws InterruptedException
Waits until the currently executing task is finished. If there is no executing task this method returns immediately.

Throws:
InterruptedException - If the caller thread gets interrupted while waiting.

joinTask

public void joinTask(long timeout)
              throws InterruptedException,
                     TimeoutException
Waits until the currently executing task is finished or the given timout period has passed (in the latter case a TimeoutException is raised). If there is no executing task this method returns immediately.

Parameters:
timeout - timeout to wait
Throws:
InterruptedException - If the caller thread gets interrupted while waiting.
TimeoutException - If the given timeout period passes and the thread is still not ready.

run

public void run()
The main loop of the worker thread. It waits for new threads passed to it by external threads which call runTask(Runnable) or runTask(Runnable, long) and executes them. It calls some callback methods defined in ThreadPool to notify about state transitions and task results, and so on.
Overrides:
run in class Thread