org.biojava.utils
Class SimpleThreadPool

java.lang.Object
  extended byorg.biojava.utils.SimpleThreadPool
All Implemented Interfaces:
ThreadPool

public class SimpleThreadPool
extends java.lang.Object
implements ThreadPool

SimpleThreadPool is a basic implementation of ThreadPool for use where we don't wish to introduce a dependency on a 3rd-party pool. In general, objects which require a pool should only use the interface and parameterize such that other implementations may be dropped in in place of this one, possibly using this one as a fallback.

This class offers a service for running Runnables using multiple threads, the number of which is specified in the constructor. Runnables are queued in a simple FIFO queue. The worker threads wait on the queue when it is empty and are notified when a new Runnable is submitted.

This implementation will prevent an application from exiting until stopThreads() is called unless the pool contains daemon threads.

Since:
1.3
Author:
Keith James

Field Summary
protected  int priority
           
protected  org.biojava.utils.SimpleThreadPool.PooledThread[] threads
           
 
Constructor Summary
SimpleThreadPool()
          Creates a new SimpleThreadPool containing 4 non-daemon threads and starts them.
SimpleThreadPool(int threadCount, boolean daemon)
          Creates a new SimpleThreadPool containing the specified number of threads and starts them.
SimpleThreadPool(int threadCount, boolean daemon, int priority)
          Creates a new SimpleThreadPool containing the specified number of threads and starts them.
 
Method Summary
 void addRequest(java.lang.Runnable task)
          addRequest requests that a Runnable be scheduled to be run by one of the threads in the pool.
protected  java.lang.Runnable nextRequest()
          nextRequest gets the next Runnable from the queue.
 int requestsQueued()
          requestsQueued returns the number of Runnables currently queued.
 void startThreads()
          startThreads starts all the threads running and opens the pool to requests.
 void stopThreads()
          stopThreads causes all running threads to stop when their current task is complete.
protected  int threadsAlive()
          threadsAlive returns the number of threads currently alive.
 int threadsIdle()
          threadsIdle returns the number of threads currently waiting for work.
 int threadsWorking()
          threadsWorking returns the number of threads currently performing work.
 void waitForThreads()
          waitForThreads temporarily closes the pool to new requests until such time as the current request queue has been emptied and all running tasks completed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

threads

protected org.biojava.utils.SimpleThreadPool.PooledThread[] threads

priority

protected int priority
Constructor Detail

SimpleThreadPool

public SimpleThreadPool()
Creates a new SimpleThreadPool containing 4 non-daemon threads and starts them. The threads have priority Thread.NORM_PRIORITY.


SimpleThreadPool

public SimpleThreadPool(int threadCount,
                        boolean daemon)
Creates a new SimpleThreadPool containing the specified number of threads and starts them. The threads have priority Thread.NORM_PRIORITY.

Parameters:
threadCount - an int thread count.
daemon - a boolean indicating whether the threads should be daemons.

SimpleThreadPool

public SimpleThreadPool(int threadCount,
                        boolean daemon,
                        int priority)
Creates a new SimpleThreadPool containing the specified number of threads and starts them.

Parameters:
threadCount - an int thread count.
daemon - a boolean indicating whether the threads should be daemons.
priority - an int priority for the threads.
Method Detail

addRequest

public void addRequest(java.lang.Runnable task)
Description copied from interface: ThreadPool
addRequest requests that a Runnable be scheduled to be run by one of the threads in the pool.

Specified by:
addRequest in interface ThreadPool
Parameters:
task - a Runnable.

startThreads

public void startThreads()
Description copied from interface: ThreadPool
startThreads starts all the threads running and opens the pool to requests.

Specified by:
startThreads in interface ThreadPool

stopThreads

public void stopThreads()
Description copied from interface: ThreadPool
stopThreads causes all running threads to stop when their current task is complete. It also closes the pool to new requests. Requests still queued are not done and the queue is emptied.

Specified by:
stopThreads in interface ThreadPool

waitForThreads

public void waitForThreads()
Description copied from interface: ThreadPool
waitForThreads temporarily closes the pool to new requests until such time as the current request queue has been emptied and all running tasks completed.

Specified by:
waitForThreads in interface ThreadPool

threadsWorking

public int threadsWorking()
threadsWorking returns the number of threads currently performing work.

Returns:
an int.

threadsIdle

public int threadsIdle()
threadsIdle returns the number of threads currently waiting for work.

Returns:
an int.

requestsQueued

public int requestsQueued()
requestsQueued returns the number of Runnables currently queued.

Returns:
an int.

threadsAlive

protected int threadsAlive()
threadsAlive returns the number of threads currently alive.

Returns:
an int.

nextRequest

protected java.lang.Runnable nextRequest()
nextRequest gets the next Runnable from the queue. This method blocks if the queue is empty and the pool has not stopped. If the pool has stopped it returns null.

Returns:
a Runnable or null if the pool has been stopped.