|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.sadun.util.tp.ThreadPool
A thread pooling class. Ensures that no more than n thread are alive at the same time, while queueing incoming requests.
The pooled threads can be created as daemon or not - if they're daemon, the JVM will exit when only pooled threads are running.
Note that a thread pool is not a thread in itself, i.e. is executed in the thread of the caller.
Use the ThreadPoolThread
for a pool which runs in a
thread on its own and on emtpy queue just sleeps.
Nested Class Summary | |
class |
ThreadPool.PooledThread
A thread class that waits() undefinitely unless explicitly notified. |
Field Summary | |
boolean |
verbose
|
Constructor Summary | |
ThreadPool(int size)
Create a pool of daemon threads with the given size and a FIFO waiting queue |
|
ThreadPool(int size,
boolean daemon)
Create a pool with the given size with a FIFO queue |
|
ThreadPool(int size,
boolean daemon,
Queue queue)
Create a pool with the given size using the given queue object. |
Method Summary | |
int |
getBusyCount()
Return an estimation of the number of threads in the pool currently associated to a runnable. |
int |
getQueueSize()
Return the number of thread currently queued |
boolean |
isAlive(java.lang.Runnable r)
Return true if the Runnable is associated with any pooled thread. |
int |
size()
Return the size of the pool |
boolean |
start(java.lang.Runnable runnable)
Add a runnable object to the pool. |
boolean |
start(java.lang.Runnable runnable,
boolean failIfNoFree)
Add a runnable object to the pool. |
void |
terminate()
Unconditionally terminates all the threads in the pool. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public boolean verbose
Constructor Detail |
public ThreadPool(int size, boolean daemon, Queue queue)
size
- the size of the pooldaemon
- if true the pools will be daemonqueue
- the queue to use for determining the next thread to
instantiate when there are pending start requests.public ThreadPool(int size, boolean daemon)
size
- the size of the pooldaemon
- if true the pools will be daemonpublic ThreadPool(int size)
size
- the size of the poolMethod Detail |
public int size()
public int getQueueSize()
public boolean start(java.lang.Runnable runnable)
If there's a thread available, the runnable is associated to the thread and started. Else, it is queued, and will run as soon as one thread becomes available.
runnable
- the Runnable object to execute
public boolean start(java.lang.Runnable runnable, boolean failIfNoFree)
If there's a thread available, the runnable is associated to the thread
and started. Else, it is queued (unless failIfNoFree is true
), and will run as soon as one thread becomes
available.
runnable
- the Runnable object to executefailIfNoFree
- if true
, returns false without queuing the request
public boolean isAlive(java.lang.Runnable r)
public int getBusyCount()
public void terminate()
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |