|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
java.lang.Objectjava.lang.ThreadGroup
net.spy.util.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).
| 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 with a normal priority. |
|
ThreadPool(String name,
int n,
int priority)
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 |
finalize()
Shuts down in case you didn't. |
int |
getActiveThreadCount()
Find out how many threads are still active (not shut down) in the pool. |
int |
getIdleThreadCount()
Find out how many threads are idle. |
int |
getMaxTaskQueueSize()
Get the maximum size of the task queue. |
int |
getMaxTotalThreads()
Get the maximum number of total threads this pool may have. |
int |
getMinIdleThreads()
Get the minimum number of idle threads. |
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. |
int |
getStartThreads()
Get the number of threads to spin up when bringing up this ThreadPool. |
int |
getTaskCount()
Find out how many tasks are in the queue. |
int |
getThreadCount()
Get the total number of threads in this pool. |
void |
setMaxTaskQueueSize(int maxTaskQueueSize)
Set the maximum size of the job queue. |
void |
setMaxTotalThreads(int maxTotalThreads)
Set the maximum number of threads that may be in this pool. |
void |
setMinIdleThreads(int minIdleThreads)
Set the minimum number of idle threads to maintain. |
void |
setMinTotalThreads(int minTotalThreads)
Set the minimum number of threads that may exist in the thread pool at any moment. |
void |
setMonitor(ThreadPoolObserver monitor)
Set the observer who will receive notification whenever a task is completed. |
void |
setPoolManagerClass(Class poolManagerClass)
Set the PoolManager class. |
void |
setPriority(int priority)
Set the priority to be used for any new threads within this threaad group. |
void |
setStartThreads(int startThreads)
Set the number of threads to start when bringing up this pool. |
void |
setTasks(LinkedList tasks)
Set the LinkedList to contain the tasks on which this ThreadPool will be listening. |
void |
shutdown()
Tell all the threads to shut down after they finish their current tasks. |
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. |
void |
waitForThreads()
Wait until there are no more threads processing. |
| Methods inherited from class java.lang.ThreadGroup |
activeCount, activeGroupCount, allowThreadSuspension, checkAccess, destroy, enumerate, enumerate, enumerate, enumerate, getMaxPriority, getName, getParent, interrupt, isDaemon, isDestroyed, list, parentOf, resume, setDaemon, setMaxPriority, stop, suspend, uncaughtException |
| 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 priority)
name - Name of the pool.n - Number of threads.
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 int getThreadCount()
public String toString()
public void setPoolManagerClass(Class poolManagerClass)
poolManagerClass - a subclass of ThreadPoolManagerpublic int getMaxTaskQueueSize()
public void setMaxTaskQueueSize(int maxTaskQueueSize)
maxTaskQueueSize - a value > 0public int getMinTotalThreads()
public void setMinTotalThreads(int minTotalThreads)
minTotalThreads - a value ≥ 0public int getMinIdleThreads()
public void setMinIdleThreads(int minIdleThreads)
minIdleThreads - a value ≥ 0public int getMaxTotalThreads()
public void setMaxTotalThreads(int maxTotalThreads)
maxTotalThreads - a value ≥ 0public int getStartThreads()
public void setStartThreads(int startThreads)
startThreads - a value ≥ 0public int getPriority()
public void setPriority(int priority)
public void setTasks(LinkedList tasks)
public ThreadPoolObserver getMonitor()
public void setMonitor(ThreadPoolObserver monitor)
public 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 fullpublic int getTaskCount()
public int getActiveThreadCount()
public void shutdown()
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
public void waitForThreads()
throws InterruptedException
IllegalStateException - if shutdown() has not been called
InterruptedException - if sleep() is interrupted
protected void finalize()
throws Throwable
Throwable
|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||