org.jscience.util
Class ThreadWorker

java.lang.Object
  extended by java.lang.Thread
      extended by org.jscience.util.ThreadWorker
All Implemented Interfaces:
java.lang.Runnable, java.util.concurrent.Executor

public class ThreadWorker
extends java.lang.Thread
implements java.util.concurrent.Executor

ThreadWorker runs tasks in a separate thread, where the tasks are queued up and performed one by one. Java 1.5 makes this class somewhat obsolete, but it's still there as it has been used elsewhere in this framework. Since the JDK 1.5 FCS, this implementation is adopted to implement Executor.

Since:
04/28/2004

Nested Class Summary
static interface ThreadWorker.Listener
          listens for tasks run by a ThreadWorker to be finished
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
ThreadWorker()
          Creates a new ThreadWorker object.
ThreadWorker(java.lang.String name)
          Creates a new ThreadWorker object.
ThreadWorker(java.lang.ThreadGroup group, java.lang.String name)
          Creates a new ThreadWorker object.
 
Method Summary
 boolean addListener(ThreadWorker.Listener listener)
          DOCUMENT ME!
 void endAfterCurrent()
          ends the thread after the current task ended; the effect is irreversible
 void endAfterLast()
          ends the thread after the last queued task ended; the effect is irreversible
 void execute(java.lang.Runnable task)
          calls runTask(task); this method enables usability with jdk1.5
 java.lang.Runnable getCurrentTask()
          returns the Runnable object currently executed by this ThreadWorker
 long getCurrentTaskRunningTime()
          if no task is currently running, -1 is returned.
 ThreadWorker.Listener[] getListeners()
          DOCUMENT ME!
 int getQueueSize()
          returns the number of Runnable objects waiting to be executed
 int getTaskCount()
          returns the number of tasks that have been executed (including the current one)
 java.lang.Runnable[] getTasks()
          returns all tasks currently in the queue (including the current one)
 long getTotalRunningTime()
          excludes time where this thread is waiting for requests
 boolean isIdle()
          DOCUMENT ME!
 boolean removeListener(ThreadWorker.Listener listener)
          DOCUMENT ME!
 boolean removeTask(java.lang.Runnable task)
          DOCUMENT ME!
 void run()
          should be called only once through start()
 int runTask(java.lang.Runnable task)
          runs the given task immediately after all previous tasks have finished - once the ThreadWorker has been started.
 boolean willEndAfterCurrent()
          DOCUMENT ME!
 boolean willEndAfterLast()
          DOCUMENT ME!
 
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

ThreadWorker

public ThreadWorker()
Creates a new ThreadWorker object.


ThreadWorker

public ThreadWorker(java.lang.String name)
Creates a new ThreadWorker object.

Parameters:
name - DOCUMENT ME!

ThreadWorker

public ThreadWorker(java.lang.ThreadGroup group,
                    java.lang.String name)
Creates a new ThreadWorker object.

Parameters:
group - DOCUMENT ME!
name - DOCUMENT ME!
Method Detail

addListener

public boolean addListener(ThreadWorker.Listener listener)
DOCUMENT ME!

Parameters:
listener - DOCUMENT ME!
Returns:
DOCUMENT ME!

removeListener

public boolean removeListener(ThreadWorker.Listener listener)
DOCUMENT ME!

Parameters:
listener - DOCUMENT ME!
Returns:
DOCUMENT ME!

getListeners

public ThreadWorker.Listener[] getListeners()
DOCUMENT ME!

Returns:
DOCUMENT ME!

getCurrentTask

public java.lang.Runnable getCurrentTask()
returns the Runnable object currently executed by this ThreadWorker

Returns:
DOCUMENT ME!

getQueueSize

public int getQueueSize()
returns the number of Runnable objects waiting to be executed

Returns:
DOCUMENT ME!

isIdle

public boolean isIdle()
DOCUMENT ME!

Returns:
DOCUMENT ME!

getTasks

public java.lang.Runnable[] getTasks()
returns all tasks currently in the queue (including the current one)

Returns:
DOCUMENT ME!

removeTask

public boolean removeTask(java.lang.Runnable task)
                   throws java.lang.IllegalStateException
DOCUMENT ME!

Parameters:
task - DOCUMENT ME!
Returns:
DOCUMENT ME!
Throws:
java.lang.IllegalStateException - if the given task is currently running

getTaskCount

public int getTaskCount()
returns the number of tasks that have been executed (including the current one)

Returns:
DOCUMENT ME!

getTotalRunningTime

public long getTotalRunningTime()
excludes time where this thread is waiting for requests

Returns:
DOCUMENT ME!

getCurrentTaskRunningTime

public long getCurrentTaskRunningTime()
if no task is currently running, -1 is returned.

Returns:
DOCUMENT ME!

execute

public void execute(java.lang.Runnable task)
calls runTask(task); this method enables usability with jdk1.5

Specified by:
execute in interface java.util.concurrent.Executor
Parameters:
task - DOCUMENT ME!

runTask

public int runTask(java.lang.Runnable task)
            throws java.lang.IllegalStateException
runs the given task immediately after all previous tasks have finished - once the ThreadWorker has been started.

Parameters:
task - DOCUMENT ME!
Returns:
the number of tasks currently in the queue (including the given one)
Throws:
java.lang.IllegalStateException - if the task has no chance anymore of being run

run

public void run()
         throws java.lang.IllegalThreadStateException
should be called only once through start()

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread
Throws:
java.lang.IllegalThreadStateException - if the method is called through anything but start() once

endAfterLast

public void endAfterLast()
ends the thread after the last queued task ended; the effect is irreversible


endAfterCurrent

public void endAfterCurrent()
ends the thread after the current task ended; the effect is irreversible


willEndAfterCurrent

public boolean willEndAfterCurrent()
DOCUMENT ME!

Returns:
DOCUMENT ME!

willEndAfterLast

public boolean willEndAfterLast()
DOCUMENT ME!

Returns:
DOCUMENT ME!