Package org.biojava.utils
Class SimpleThreadPool
- java.lang.Object
-
- org.biojava.utils.SimpleThreadPool
-
- All Implemented Interfaces:
ThreadPool
public class SimpleThreadPool extends Object implements ThreadPool
SimpleThreadPoolis a basic implementation ofThreadPoolfor 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 newRunnableis 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
-
-
Constructor Summary
Constructors Constructor Description SimpleThreadPool()Creates a newSimpleThreadPoolcontaining 4 non-daemon threads and starts them.SimpleThreadPool(int threadCount, boolean daemon)Creates a newSimpleThreadPoolcontaining the specified number of threads and starts them.SimpleThreadPool(int threadCount, boolean daemon, int priority)Creates a newSimpleThreadPoolcontaining the specified number of threads and starts them.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddRequest(Runnable task)addRequestrequests that aRunnablebe scheduled to be run by one of the threads in the pool.protected RunnablenextRequest()nextRequestgets the nextRunnablefrom the queue.intrequestsQueued()requestsQueuedreturns the number ofRunnables currently queued.voidstartThreads()startThreadsstarts all the threads running and opens the pool to requests.voidstopThreads()Waits for all working threads to return and then stops them.protected intthreadsAlive()threadsAlivereturns the number of threads currently alive.intthreadsIdle()threadsIdlereturns the number of threads currently waiting for work.intthreadsWorking()threadsWorkingreturns the number of threads currently performing work.voidwaitForThreads()waitForThreadstemporarily closes the pool to new requests until such time as the current request queue has been emptied and all running tasks completed.
-
-
-
Constructor Detail
-
SimpleThreadPool
public SimpleThreadPool()
Creates a newSimpleThreadPoolcontaining 4 non-daemon threads and starts them. The threads have priority Thread.NORM_PRIORITY. Because threads are non-deamon you will need to call stopThreads() to terminate them.
-
SimpleThreadPool
public SimpleThreadPool(int threadCount, boolean daemon)
Creates a newSimpleThreadPoolcontaining the specified number of threads and starts them. The threads have priority Thread.NORM_PRIORITY.- Parameters:
threadCount- anintthread count.daemon- abooleanindicating whether the threads should be daemons. If threads are non-deamon you will need to call stopThreads() to terminate them.
-
SimpleThreadPool
public SimpleThreadPool(int threadCount, boolean daemon, int priority)
Creates a newSimpleThreadPoolcontaining the specified number of threads and starts them.- Parameters:
threadCount- anintthread count.daemon- abooleanindicating whether the threads should be daemons. If threads are non-deamon you will need to call stopThreads() to terminate them.priority- anintpriority for the threads.
-
-
Method Detail
-
addRequest
public void addRequest(Runnable task)
Description copied from interface:ThreadPooladdRequestrequests that aRunnablebe scheduled to be run by one of the threads in the pool.- Specified by:
addRequestin interfaceThreadPool- Parameters:
task- aRunnable.
-
startThreads
public void startThreads()
Description copied from interface:ThreadPoolstartThreadsstarts all the threads running and opens the pool to requests.- Specified by:
startThreadsin interfaceThreadPool
-
stopThreads
public void stopThreads()
Waits for all working threads to return and then stops them. If the thread pool contains non-daemon threads you will have to call this method to make your program return.- Specified by:
stopThreadsin interfaceThreadPool- Throws:
IllegalStateException- if the pool is already stopped.
-
waitForThreads
public void waitForThreads()
Description copied from interface:ThreadPoolwaitForThreadstemporarily closes the pool to new requests until such time as the current request queue has been emptied and all running tasks completed.- Specified by:
waitForThreadsin interfaceThreadPool
-
threadsWorking
public int threadsWorking()
threadsWorkingreturns the number of threads currently performing work.- Returns:
- an
int.
-
threadsIdle
public int threadsIdle()
threadsIdlereturns the number of threads currently waiting for work.- Returns:
- an
int.
-
requestsQueued
public int requestsQueued()
requestsQueuedreturns the number ofRunnables currently queued.- Returns:
- an
int.
-
threadsAlive
protected int threadsAlive()
threadsAlivereturns the number of threads currently alive.- Returns:
- an
int.
-
nextRequest
protected Runnable nextRequest()
nextRequestgets the nextRunnablefrom 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
Runnableor null if the pool has been stopped.
-
-