edu.cuny.cat.task
Class PriorityAsynchronousDispatcher

java.lang.Object
  extended by java.lang.Thread
      extended by edu.cuny.cat.task.PriorityAsynchronousDispatcher
All Implemented Interfaces:
Dispatcher, java.lang.Runnable

public class PriorityAsynchronousDispatcher
extends java.lang.Thread
implements Dispatcher

An asynchronous dispatcher that runs a thread to process tasks. It allows to use a task comparator and the task IDs to support a stable priority queue of tasks.

Version:
$Revision: 1.2 $
Author:
Jinzhong Niu
See Also:
PrioritySynchronousDispatcher

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
protected  int counter
           
protected static org.apache.log4j.Logger logger
           
protected  java.util.PriorityQueue<DispatchingTask> tasks
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
PriorityAsynchronousDispatcher(java.util.Comparator<DispatchingTask> comparator)
           
 
Method Summary
 void addTask(DispatchingTask task)
          adds a dispatching task to process.
 DispatchingTask[] getTasks()
           
 void process()
          to process the pending tasks.
 void processTask(DispatchingTask task)
          to add the task and process all pending tasks.
 void run()
           
 void terminate()
          terminates the dispatcher.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, 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, yield
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

logger

protected static org.apache.log4j.Logger logger

tasks

protected final java.util.PriorityQueue<DispatchingTask> tasks

counter

protected int counter
Constructor Detail

PriorityAsynchronousDispatcher

public PriorityAsynchronousDispatcher(java.util.Comparator<DispatchingTask> comparator)
Method Detail

run

public void run()
Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

addTask

public void addTask(DispatchingTask task)
Description copied from interface: Dispatcher
adds a dispatching task to process.

Specified by:
addTask in interface Dispatcher
Parameters:
task - task to process

process

public void process()
Description copied from interface: Dispatcher
to process the pending tasks.

Specified by:
process in interface Dispatcher

processTask

public void processTask(DispatchingTask task)
Description copied from interface: Dispatcher
to add the task and process all pending tasks. This does exactly what Dispatcher.addTask(DispatchingTask) and Dispatcher.process() do together.

Specified by:
processTask in interface Dispatcher

terminate

public void terminate()
Description copied from interface: Dispatcher
terminates the dispatcher.

Specified by:
terminate in interface Dispatcher

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Thread

getTasks

public DispatchingTask[] getTasks()
Specified by:
getTasks in interface Dispatcher
Returns:
the array of tasks yet to process.