public class SimpleThreadPool extends 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 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.
Modifier and Type | Field and Description |
---|---|
protected int |
priority |
protected org.biojava.utils.SimpleThreadPool.PooledThread[] |
threads |
Constructor and Description |
---|
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. |
Modifier and Type | Method and Description |
---|---|
void |
addRequest(Runnable task)
addRequest requests that a Runnable
be scheduled to be run by one of the threads in the pool. |
protected Runnable |
nextRequest()
nextRequest gets the next Runnable
from the queue. |
int |
requestsQueued()
requestsQueued returns the number of
Runnable s currently queued. |
void |
startThreads()
startThreads starts all the threads running and
opens the pool to requests. |
void |
stopThreads()
Waits for all working threads to return and then stops them.
|
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. |
protected org.biojava.utils.SimpleThreadPool.PooledThread[] threads
protected int priority
public SimpleThreadPool()
SimpleThreadPool
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.public SimpleThreadPool(int threadCount, boolean daemon)
SimpleThreadPool
containing the
specified number of threads and starts them. The threads have
priority Thread.NORM_PRIORITY.threadCount
- an int
thread count.daemon
- a boolean
indicating whether the
threads should be daemons. If threads are non-deamon you will need
to call stopThreads() to terminate them.public SimpleThreadPool(int threadCount, boolean daemon, int priority)
SimpleThreadPool
containing the
specified number of threads and starts them.threadCount
- an int
thread count.daemon
- a boolean
indicating whether the
threads should be daemons. If threads are non-deamon you will need
to call stopThreads() to terminate them.priority
- an int
priority for the threads.public void addRequest(Runnable task)
ThreadPool
addRequest
requests that a Runnable
be scheduled to be run by one of the threads in the pool.addRequest
in interface ThreadPool
task
- a Runnable
.public void startThreads()
ThreadPool
startThreads
starts all the threads running and
opens the pool to requests.startThreads
in interface ThreadPool
public void stopThreads()
stopThreads
in interface ThreadPool
IllegalStateException
- if the pool is already stopped.public void waitForThreads()
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.waitForThreads
in interface ThreadPool
public int threadsWorking()
threadsWorking
returns the number of threads
currently performing work.int
.public int threadsIdle()
threadsIdle
returns the number of threads
currently waiting for work.int
.public int requestsQueued()
requestsQueued
returns the number of
Runnable
s currently queued.int
.protected int threadsAlive()
threadsAlive
returns the number of threads
currently alive.int
.protected 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.Runnable
or null if the pool has been
stopped.Copyright © 2014 BioJava. All rights reserved.