|
||||||||||
| 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
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).
| Constructor Summary | |
|---|---|
ThreadPool(java.lang.String name)
Get an instance of ThreadPool with five threads and a normal priority. |
|
ThreadPool(java.lang.String name,
int n)
Get an instance of ThreadPool with a normal priority. |
|
ThreadPool(java.lang.String name,
int n,
int prio)
Get an instance of ThreadPool. |
|
| Method Summary | |
|---|---|
void |
addTask(java.lang.Runnable r)
Add a task for one of the threads to execute. |
boolean |
addTask(java.lang.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 mtqs)
Set the maximum size of the job queue. |
void |
setMaxTotalThreads(int mtt)
Set the maximum number of threads that may be in this pool. |
void |
setMinIdleThreads(int mit)
Set the minimum number of idle threads to maintain. |
void |
setMinTotalThreads(int mtt)
Set the minimum number of threads that may exist in the thread pool at any moment. |
void |
setMonitor(ThreadPoolObserver m)
Set the observer who will receive notification whenever a task is completed. |
void |
setPoolManagerClass(java.lang.Class pmc)
Set the PoolManager class. |
void |
setPriority(int p)
Set the priority to be used for any new threads within this threaad group. |
void |
setStartThreads(int st)
Set the number of threads to start when bringing up this pool. |
void |
setTasks(java.util.LinkedList<net.spy.util.ThreadPool.Task> t)
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. |
java.lang.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(java.lang.String name,
int n,
int prio)
name - Name of the pool.n - Number of threads.prio - Priority of the child threads.
public ThreadPool(java.lang.String name,
int n)
name - Name of the pool.n - Number of threads.public ThreadPool(java.lang.String name)
name - Name of the pool.| Method Detail |
|---|
public void start()
public int getIdleThreadCount()
public int getThreadCount()
public java.lang.String toString()
toString in class java.lang.ThreadGrouppublic void setPoolManagerClass(java.lang.Class pmc)
pmc - a subclass of ThreadPoolManagerpublic int getMaxTaskQueueSize()
public void setMaxTaskQueueSize(int mtqs)
mtqs - a value > 0public int getMinTotalThreads()
public void setMinTotalThreads(int mtt)
mtt - a value ≥ 0public int getMinIdleThreads()
public void setMinIdleThreads(int mit)
mit - a value ≥ 0public int getMaxTotalThreads()
public void setMaxTotalThreads(int mtt)
mtt - a value ≥ 0public int getStartThreads()
public void setStartThreads(int st)
st - a value ≥ 0public int getPriority()
public void setPriority(int p)
public void setTasks(java.util.LinkedList<net.spy.util.ThreadPool.Task> t)
public ThreadPoolObserver getMonitor()
public void setMonitor(ThreadPoolObserver m)
public void addTask(java.lang.Runnable r)
java.lang.IndexOutOfBoundsException - if the backing list is fullThreadPoolRunnable
public boolean addTask(java.lang.Runnable r,
long timeout)
r - the task to executetimeout - the number of milliseconds to wait for it to start
java.lang.IndexOutOfBoundsException - if the backing list is fullpublic int getTaskCount()
public int getActiveThreadCount()
public void shutdown()
public void waitForCompletion()
throws java.lang.InterruptedException
This is a convenience method that calls waitforTaskCount(0), followed by shutdown(), followed by waitForThreads().
java.lang.InterruptedException - if waitForTaskCount or waitForThreads
throws an exception
public void waitForTaskCount(int num)
throws java.lang.InterruptedException
num - the number of tasks for which to wait
java.lang.InterruptedException - if wait fails
public void waitForThreads()
throws java.lang.InterruptedException
java.lang.IllegalStateException - if shutdown() has not been called
java.lang.InterruptedException - if sleep() is interrupted
protected void finalize()
throws java.lang.Throwable
finalize in class java.lang.Objectjava.lang.Throwable
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||