org.sadun.util.tp
Class ThreadPool

java.lang.Object
  extended byorg.sadun.util.tp.ThreadPool

public class ThreadPool
extends java.lang.Object

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.

Version:
1.1
Author:
Cris Sadun

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

verbose

public boolean verbose
Constructor Detail

ThreadPool

public ThreadPool(int size,
                  boolean daemon,
                  Queue queue)
Create a pool with the given size using the given queue object.

Parameters:
size - the size of the pool
daemon - if true the pools will be daemon
queue - the queue to use for determining the next thread to instantiate when there are pending start requests.

ThreadPool

public ThreadPool(int size,
                  boolean daemon)
Create a pool with the given size with a FIFO queue

Parameters:
size - the size of the pool
daemon - if true the pools will be daemon

ThreadPool

public ThreadPool(int size)
Create a pool of daemon threads with the given size and a FIFO waiting queue

Parameters:
size - the size of the pool
Method Detail

size

public int size()
Return the size of the pool

Returns:
the size of the pool

getQueueSize

public int getQueueSize()
Return the number of thread currently queued

Returns:
the number of thread currently queued

start

public boolean start(java.lang.Runnable runnable)
Add a runnable object to the pool.

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.

Parameters:
runnable - the Runnable object to execute
Returns:
true if the runnable is started, false if it's queued.

start

public boolean start(java.lang.Runnable runnable,
                     boolean failIfNoFree)
Add a runnable object to the pool.

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.

Parameters:
runnable - the Runnable object to execute
failIfNoFree - if true, returns false without queuing the request
Returns:
true if the runnable is started, false if it's not started.

isAlive

public boolean isAlive(java.lang.Runnable r)
Return true if the Runnable is associated with any pooled thread.

Returns:
true if the Runnable is associated with any pooled thread.

getBusyCount

public int getBusyCount()
Return an estimation of the number of threads in the pool currently associated to a runnable.

Returns:
an estimation of the number of threads in the pool currently associated to a runnable.

terminate

public void terminate()
Unconditionally terminates all the threads in the pool. Free threads simply exit. Threads associated to a Runnable are interrupt()ed.