de.nava.informa.utils.toolkit
Class WorkerThread

java.lang.Object
  extended by java.lang.Thread
      extended by de.nava.informa.utils.toolkit.WorkerThread
All Implemented Interfaces:
Runnable
Direct Known Subclasses:
CleanerWorkerThread, PollerWorkerThread

public abstract class WorkerThread
extends Thread

Abstract worker thread which is driven by WorkersManager. Worker thread runs until terminate property is set to true. All of the threads are run in daemon mode and do not prevent application from sudden quit.

In order to do something useful with channel this class should be extended with implementation of process() method. This method is invoked each time the thread receives new processing task. Current class takes care of getting new tasks, marking busy state and other necessary tasks.

After the task is finished worker checks JobSourceIF object (if specified) to get new assignments and it there's no job goes to sleep for a IDLE_SLEEP_TIME milliseconds. When manager finds new task it immediately awakens the thread. If the thread doesn't receive new assignments for an IDLE_SLEEP_TIME it checks the JobSourceIF object again by itself to find new task. This is done for greater safety reasons to avoid situations when the queue is full of tasks and workers are in endless sleep.

Author:
Aleksey Gureev (spyromus@noizeramp.com)

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
WorkerThread(String name)
          Creates named worker thread.
 
Method Summary
 ChannelRecord getChannelInProcess()
          Returns channel which is currently in processing.
protected abstract  void processRecord(ChannelRecord record)
          Processes record.
 void run()
          If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.
 void setQueue(org.apache.commons.collections.Buffer queue)
          Sets the queue of tasks.
 void terminate()
          Marks this thread for termination on job completion.
 
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
 

Constructor Detail

WorkerThread

public WorkerThread(String name)
Creates named worker thread.

Parameters:
name - name of the thread.
Method Detail

terminate

public final void terminate()
Marks this thread for termination on job completion.


getChannelInProcess

public final ChannelRecord getChannelInProcess()
Returns channel which is currently in processing.

Returns:
channel.

run

public final void run()
If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.

Subclasses of Thread should override this method.

Specified by:
run in interface Runnable
Overrides:
run in class Thread
See Also:
Thread.start(), Thread.Thread(ThreadGroup, Runnable, String), Runnable.run()

processRecord

protected abstract void processRecord(ChannelRecord record)
Processes record.

Parameters:
record - record to process.

setQueue

public void setQueue(org.apache.commons.collections.Buffer queue)
Sets the queue of tasks.

Parameters:
queue - blocking queue of tasks.


Copyright © 2002-2007 Niko Schmuck. All Rights Reserved.