Package org.biojava.utils
Class SimpleThreadPool
java.lang.Object
org.biojava.utils.SimpleThreadPool
- All Implemented Interfaces:
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 Runnable
s
using multiple threads, the number of which is specified in the
constructor. Runnable
s 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
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates a newSimpleThreadPool
containing 4 non-daemon threads and starts them.SimpleThreadPool
(int threadCount, boolean daemon) Creates a newSimpleThreadPool
containing the specified number of threads and starts them.SimpleThreadPool
(int threadCount, boolean daemon, int priority) Creates a newSimpleThreadPool
containing the specified number of threads and starts them. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addRequest
(Runnable task) addRequest
requests that aRunnable
be scheduled to be run by one of the threads in the pool.protected Runnable
nextRequest
gets the nextRunnable
from the queue.int
requestsQueued
returns the number ofRunnable
s currently queued.void
startThreads
starts all the threads running and opens the pool to requests.void
Waits for all working threads to return and then stops them.protected int
threadsAlive
returns the number of threads currently alive.int
threadsIdle
returns the number of threads currently waiting for work.int
threadsWorking
returns the number of threads currently performing work.void
waitForThreads
temporarily closes the pool to new requests until such time as the current request queue has been emptied and all running tasks completed.
-
Field Details
-
threads
-
priority
-
-
Constructor Details
-
SimpleThreadPool
public SimpleThreadPool()Creates a newSimpleThreadPool
containing 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
Creates a newSimpleThreadPool
containing the specified number of threads and starts them. The threads have priority Thread.NORM_PRIORITY.- Parameters:
threadCount
- anint
thread count.daemon
- aboolean
indicating whether the threads should be daemons. If threads are non-deamon you will need to call stopThreads() to terminate them.
-
SimpleThreadPool
Creates a newSimpleThreadPool
containing the specified number of threads and starts them.- Parameters:
threadCount
- anint
thread count.daemon
- aboolean
indicating whether the threads should be daemons. If threads are non-deamon you will need to call stopThreads() to terminate them.priority
- anint
priority for the threads.
-
-
Method Details
-
addRequest
Description copied from interface:ThreadPool
addRequest
requests that aRunnable
be scheduled to be run by one of the threads in the pool.- Specified by:
addRequest
in interfaceThreadPool
- Parameters:
task
- aRunnable
.
-
startThreads
Description copied from interface:ThreadPool
startThreads
starts all the threads running and opens the pool to requests.- Specified by:
startThreads
in interfaceThreadPool
-
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:
stopThreads
in interfaceThreadPool
- Throws:
IllegalStateException
- if the pool is already stopped.
-
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 interfaceThreadPool
-
threadsWorking
threadsWorking
returns the number of threads currently performing work.- Returns:
- an
int
.
-
threadsIdle
threadsIdle
returns the number of threads currently waiting for work.- Returns:
- an
int
.
-
requestsQueued
requestsQueued
returns the number ofRunnable
s currently queued.- Returns:
- an
int
.
-
threadsAlive
threadsAlive
returns the number of threads currently alive.- Returns:
- an
int
.
-
nextRequest
nextRequest
gets the nextRunnable
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.
-