Holger's
Java API

com.antelmann.util
Class ThreadWorker

java.lang.Object
  extended by java.lang.Thread
      extended by com.antelmann.util.ThreadWorker
All Implemented Interfaces:
Runnable, Executor

public class ThreadWorker
extends Thread
implements 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 - plus it has additional functionality that the standard JDK implementation has not. Since the JDK 1.5 FCS, this implementation is adopted to implement Executor.

Since:
04/28/2004
Author:
Holger Antelmann
See Also:
ThreadPool

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
Thread.State, Thread.UncaughtExceptionHandler
 
Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
ThreadWorker()
           
ThreadWorker(String name)
           
ThreadWorker(ThreadGroup group, String name)
           
 
Method Summary
 ThreadWorker.Listener addExceptionHandler(Thread.UncaughtExceptionHandler handler)
           
 boolean addListener(ThreadWorker.Listener listener)
           
static Runnable asRunnable(Task<?> task)
           
 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(Runnable task)
          calls runTask(task); this method enables usability with jdk1.5
 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()
           
 int getQueueSize()
          returns the number of Runnable objects waiting to be executed
 int getTaskCount()
          returns the number of tasks that have already been executed
 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()
           
 boolean isReady()
          if ready, this instance will accept new tasks
 boolean removeListener(ThreadWorker.Listener listener)
           
 boolean removeTask(Runnable task)
           
 void run()
          should be called only once through start()
 int runTask(Runnable task)
          runs the given task immediately after all previous tasks have finished - once the ThreadWorker has been started.
 int runTask(Task<?> task)
           
 void waitFor(Runnable myTask)
          waits until the given task has been executed on this Executor
 void waitForActiveQueue()
          waits until all currently scheduled tasks have been executed
 boolean willEndAfterCurrent()
           
 boolean willEndAfterLast()
           
 
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, toString, yield
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ThreadWorker

public ThreadWorker()

ThreadWorker

public ThreadWorker(String name)

ThreadWorker

public ThreadWorker(ThreadGroup group,
                    String name)
Method Detail

addExceptionHandler

public ThreadWorker.Listener addExceptionHandler(Thread.UncaughtExceptionHandler handler)

addListener

public boolean addListener(ThreadWorker.Listener listener)

removeListener

public boolean removeListener(ThreadWorker.Listener listener)

getListeners

public ThreadWorker.Listener[] getListeners()

getCurrentTask

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


getQueueSize

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


isIdle

public boolean isIdle()

getTasks

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


removeTask

public boolean removeTask(Runnable task)
                   throws IllegalStateException
Throws:
IllegalStateException - if the given task is currently running

getTaskCount

public int getTaskCount()
returns the number of tasks that have already been executed


getTotalRunningTime

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


getCurrentTaskRunningTime

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


execute

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

Specified by:
execute in interface Executor

runTask

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

Returns:
the number of tasks currently in the queue (including the given one)
Throws:
IllegalStateException - if the task has no chance anymore of being run

runTask

public int runTask(Task<?> task)
            throws IllegalStateException
Throws:
IllegalStateException
See Also:
runTask(Runnable)

asRunnable

public static Runnable asRunnable(Task<?> task)

run

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

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

waitForActiveQueue

public void waitForActiveQueue()
waits until all currently scheduled tasks have been executed


waitFor

public void waitFor(Runnable myTask)
waits until the given task has been executed on this Executor


isReady

public boolean isReady()
if ready, this instance will accept new tasks


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()

willEndAfterLast

public boolean willEndAfterLast()


(c) Holger Antelmann since 2001- all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads