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. Since the JDK 1.5 FCS, this implementation is adopted to implement Executor.

Since:
04/28/2004
Author:
Holger Antelmann

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
 boolean addListener(ThreadWorker.Listener listener)
           
 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 been executed (including the current one)
 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 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.
 boolean willEndAfterCurrent()
           
 boolean willEndAfterLast()
           
 
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()

ThreadWorker

public ThreadWorker(String name)

ThreadWorker

public ThreadWorker(ThreadGroup group,
                    String name)
Method Detail

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 been executed (including the current one)


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

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

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) 2001-2006 Holger Antelmann - all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads