org.jscience.util
Class ThreadPool

java.lang.Object
  extended by org.jscience.util.ThreadPool
All Implemented Interfaces:
java.util.concurrent.Executor

public class ThreadPool
extends java.lang.Object
implements java.util.concurrent.Executor

uses a pool of threads to execute tasks asynchronously and concurrently. ThreadPool was implemented before JDK1.5 and is somewhat simmilar to java.util.concurrent.ThreadPoolExecutor


Constructor Summary
ThreadPool(int numberOfThreads)
          Creates a new ThreadPool object.
ThreadPool(int numberOfThreads, boolean asDaemon)
          Creates a new ThreadPool object.
 
Method Summary
 void addListener(ThreadWorker.Listener listener)
          DOCUMENT ME!
 void addThreadWorker()
          adds one thread to the pool
 void execute(java.lang.Runnable task)
          calls runTask(task); this method enables usability with jdk1.5
 void finishAll(boolean now)
          removes all ThreadWorkers, which will all end either after the current or the last queued task - depending on the 'now' parameter.
 ThreadWorker.Listener[] getListeners()
          DOCUMENT ME!
 int getNumberOfThreads()
          returns the number of all threads currently in this pool
 int getQueueSize()
          total queue size over all threads
 int idleThreads()
          returns the number of threads that are currently idle
 boolean isActive()
          returns true if at least one thread in the pool is currently active
 boolean removeIdleThread()
          attempts to remove a single ThreadWorker from the pool.
 void removeIdleThreads()
          removes all threads that are currently idle.
 void removeListener(ThreadWorker.Listener listener)
          DOCUMENT ME!
 void runTask(java.lang.Runnable task)
          DOCUMENT ME!
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ThreadPool

public ThreadPool(int numberOfThreads)
Creates a new ThreadPool object.

Parameters:
numberOfThreads - DOCUMENT ME!

ThreadPool

public ThreadPool(int numberOfThreads,
                  boolean asDaemon)
Creates a new ThreadPool object.

Parameters:
numberOfThreads - DOCUMENT ME!
asDaemon - DOCUMENT ME!
Method Detail

isActive

public boolean isActive()
returns true if at least one thread in the pool is currently active

Returns:
DOCUMENT ME!

getNumberOfThreads

public int getNumberOfThreads()
returns the number of all threads currently in this pool

Returns:
DOCUMENT ME!

addThreadWorker

public void addThreadWorker()
adds one thread to the pool


idleThreads

public int idleThreads()
returns the number of threads that are currently idle

Returns:
DOCUMENT ME!

removeIdleThreads

public void removeIdleThreads()
removes all threads that are currently idle. A thread can only be removed if idle.


removeIdleThread

public boolean removeIdleThread()
attempts to remove a single ThreadWorker from the pool. If all threads but one have already been removed from the pool, nothing happens and false is returned. This method first attempts to remove an idle thread - if found. If all threads are active, the one with the smallest queue size is removed.

Returns:
true only if a thread was indeed removed from the pool

finishAll

public void finishAll(boolean now)
removes all ThreadWorkers, which will all end either after the current or the last queued task - depending on the 'now' parameter.

Parameters:
now - DOCUMENT ME!

getQueueSize

public int getQueueSize()
total queue size over all threads

Returns:
DOCUMENT ME!

addListener

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

Parameters:
listener - DOCUMENT ME!

getListeners

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

Returns:
DOCUMENT ME!

removeListener

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

Parameters:
listener - 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 void runTask(java.lang.Runnable task)
             throws java.lang.IllegalStateException
DOCUMENT ME!

Parameters:
task - DOCUMENT ME!
Throws:
java.lang.IllegalStateException - DOCUMENT ME!