|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.util.concurrent.AbstractExecutorService
java.util.concurrent.ThreadPoolExecutor
net.spy.concurrent.ThreadPool
public class ThreadPool
A producer/consumer thread pool for easy parallelism.
Quick start example (assuming you want to do a million tasks, 15 at time):
// Get a thread pool that will perform 15 tasks at a time
ThreadPool tp=new ThreadPool("Test Pool", 15);
// Start the thread pool
tp.start();
// Do the tasks in a loop, throttling to make sure all we don't
// create too many objects that aren't ready to be used.
for(int i=0; i<1000000; i++) {
// Don't have more than 32 unclaimed tasks
tp.waitForTaskCount(32);
tp.addTask(new MyRunnableClass());
}
tp.waitForCompletion();
The ThreadPoolManager instance is responsible for sizing and resizing the pool. On start(), the ThreadPoolManager will size the pool to the start size configured in the ThreadPool. The manager will receive a notification after any task is added to the pool and may choose to take action at that point. Otherwise, it will sleep for a certain amount of time (one minute by default) and then begin its main loop verifying the number of idle threads satisfies the configuration.
If the number of idle threads is too low, it will create as many as is required to have the appropriate number of idle threads as long as the total number of threads will not exceed the configured maximum.
If the number of idle threads is too high, it will reduce the total number of threads by one (for each loop).
| Nested Class Summary |
|---|
| Nested classes/interfaces inherited from class java.util.concurrent.ThreadPoolExecutor |
|---|
ThreadPoolExecutor.AbortPolicy, ThreadPoolExecutor.CallerRunsPolicy, ThreadPoolExecutor.DiscardOldestPolicy, ThreadPoolExecutor.DiscardPolicy |
| Constructor Summary | |
|---|---|
ThreadPool(String name)
Get an instance of ThreadPool with five threads and a normal priority. |
|
ThreadPool(String name,
int n)
Get an instance of ThreadPool. |
|
ThreadPool(String name,
int n,
int max)
Get an instance of ThreadPool. |
|
ThreadPool(String name,
int n,
int max,
int prio)
Get an instance of ThreadPool. |
|
ThreadPool(String name,
int n,
int max,
int prio,
BlockingQueue<Runnable> q)
Get an instance of ThreadPool. |
|
ThreadPool(String name,
int n,
int max,
int prio,
int size)
Get an instance of ThreadPool. |
|
| Method Summary | |
|---|---|
void |
addTask(Runnable r)
Add a task for one of the threads to execute. |
boolean |
addTask(Runnable r,
long timeout)
Add a task for one of the threads to execute. |
protected void |
afterExecute(Runnable r,
Throwable t)
|
protected void |
beforeExecute(Thread t,
Runnable r)
|
int |
getIdleThreadCount()
Find out how many threads are idle. |
int |
getMinTotalThreads()
Get the minimum number of threads that may exist in the thread pool at any moment. |
ThreadPoolObserver |
getMonitor()
Get the monitor that receives notifications when a worker thread finishes a job. |
int |
getPriority()
Get the priority that will be used for any new threads within this thread group. |
void |
setMonitor(ThreadPoolObserver m)
Set the observer who will receive notification whenever a task is completed. |
void |
setPriority(int p)
Set the priority to be used for any new threads within this threaad group. |
void |
start()
Start the ThreadPool. |
String |
toString()
String me. |
void |
waitForCompletion()
Shut down all of the threads after all jobs are complete, and wait for all tasks to complete. |
void |
waitForTaskCount(int num)
Wait until there are no more than num tasks in the queue. |
| Methods inherited from class java.util.concurrent.AbstractExecutorService |
|---|
invokeAll, invokeAll, invokeAny, invokeAny, submit, submit, submit |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
|---|
public ThreadPool(String name,
int n,
int max,
int prio,
BlockingQueue<Runnable> q)
name - the name of this pooln - the core size of this poolmax - the maximum size of this poolprio - the priority of threads created within this poolq - the work queue
public ThreadPool(String name,
int n,
int max,
int prio,
int size)
name - Name of the pool.n - number of threadsmax - the maximum number of threadsprio - Priority of the child threads.size - the queue size (as an ArrayBlockingQueue)
public ThreadPool(String name,
int n,
int max,
int prio)
name - name of the pooln - core pool sizemax - max pool sizeprio - priority of threads created within this pool
public ThreadPool(String name,
int n,
int max)
name - name of the pooln - core pool sizemax - max pool size
public ThreadPool(String name,
int n)
name - Name of the pool.n - Number of threads.public ThreadPool(String name)
name - Name of the pool.| Method Detail |
|---|
public void start()
public int getIdleThreadCount()
public String toString()
toString in class Objectpublic int getMinTotalThreads()
public int getPriority()
public void setPriority(int p)
public ThreadPoolObserver getMonitor()
public void setMonitor(ThreadPoolObserver m)
protected void beforeExecute(Thread t,
Runnable r)
beforeExecute in class ThreadPoolExecutor
protected void afterExecute(Runnable r,
Throwable t)
afterExecute in class ThreadPoolExecutorpublic void addTask(Runnable r)
IndexOutOfBoundsException - if the backing list is fullThreadPoolRunnable
public boolean addTask(Runnable r,
long timeout)
r - the task to executetimeout - the number of milliseconds to wait for it to start
IndexOutOfBoundsException - if the backing list is full
public void waitForCompletion()
throws InterruptedException
This is a convenience method that calls waitforTaskCount(0), followed by shutdown(), followed by waitForThreads().
InterruptedException - if waitForTaskCount or waitForThreads
throws an exception
public void waitForTaskCount(int num)
throws InterruptedException
num - the number of tasks for which to wait
InterruptedException - if wait fails
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||