|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Object | +--com.mischiefbox.pollserve.ThreadPool
Implements a simple thread pool.
Tracks the in-use and available processor threads, holds onto idle processor threads, and creates new processor threads up to the maximum number of processor threads permitted. Releases idle processor threads after a specified period of time. Provides processor threads to callers.
Management is accomplished by a manager thread that examines the state of the thread pool occasionally.
| Field Summary | |
protected boolean |
bManaging
Indicates if this is managing the thread pool. |
protected float |
fIdleRatio
Idle thread ratio. |
protected int |
iIdleLifetime
The maximum idle thread lifetime. |
protected int |
iMaxThreads
The maximum number of threads in the thread pool. |
protected Queue |
qThreads
The available thread pool. |
protected static int |
SLEEP_MILLIS
Time between thread pool checks. |
protected Thread |
tManager
The management thread. |
protected Vector |
vThreads
The complete thread pool. |
| Constructor Summary | |
ThreadPool(int iMaxThreads)
Create a new thread pool. |
|
ThreadPool(int iMaxThreads,
float fIdleRatio)
Create a new thread pool and specify the idle ratio. |
|
ThreadPool(int iMaxThreads,
float fIdleRatio,
int iIdleLifetime)
Create a new thread pool, specify the idle ratio, and indicate the maximum lifetime of an idle thread. |
|
| Method Summary | |
ProcessorThread |
getFree()
Get the next free thread from the pool. |
ProcessorThread |
nextFree()
Get the next free thread from the pool. |
void |
nowFree(ProcessorThread pt)
Set a processor thread as free (idle). |
void |
registerProcessorThread(ProcessorThread pt)
Register a newly created processor thread with the pool. |
void |
run()
Manage the processor thread pool. |
void |
shutdown()
Shut down the thread pool. |
void |
unregisterProcessorThread(ProcessorThread pt)
Unregister a processor thread from the pool. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
protected static int SLEEP_MILLIS
protected float fIdleRatio
To create as many threads as the maximum specified, set this to zero (which will disable thread management).
The default of 0.7 means that for 10 threads in the system, seven should be active at a time. If eight active threads are needed, a new idle thread will be created.
protected int iIdleLifetime
protected int iMaxThreads
protected Thread tManager
protected Queue qThreads
protected Vector vThreads
protected boolean bManaging
| Constructor Detail |
public ThreadPool(int iMaxThreads)
iMaxThreads - the maximum number of threads in the thread
pool.
public ThreadPool(int iMaxThreads,
float fIdleRatio)
iMaxThreads - the maximum number of threads in the thread
pool.fIdleRatio - the ratio of active threads to idle threads.
public ThreadPool(int iMaxThreads,
float fIdleRatio,
int iIdleLifetime)
iMaxThreads - the maximum number of threads in the thread
pool.fIdleRatio - the ratio of active threads to idle threads.iIdleLifetime - the maximum number of seconds a thread
may be in the idle state before being
killed.| Method Detail |
public void shutdown()
Synchronized.
public void registerProcessorThread(ProcessorThread pt)
Synchronized access to the pool.
pt - the new processor thread.public void unregisterProcessorThread(ProcessorThread pt)
Synchronized access to the pool.
pt - the processor thread to remove.public ProcessorThread nextFree()
public void nowFree(ProcessorThread pt)
Should only be called by the ProcessorThread.
pt - the idle processor thread.
public ProcessorThread getFree()
throws NoSuchElementException
NoSuchElementException - when there is no free processor
thread.public void run()
If we are managing threads, and if there are not enough idle threads, and if the thread pool maximum size is not already hit, create a new thread. If we are not managing threads, then make sure that there are enough threads in the pool, then end the loop.
If there are more idle threads than needed (and we care about the thread lifetime), then examine the idle threads and remove those that have exceeded their lifetimes.
run in interface Runnable
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||