org.xlattice.util.threads
Class ThreadPool

java.lang.Object
  extended by org.xlattice.util.threads.ThreadPool

public class ThreadPool
extends java.lang.Object

Fixed size pool of Killable threads which consume a (possibly variable in size) queue of Killable jobs, Runnables with a die() method. XXX Modify to allow change in pool capacity.

Author:
Jim Dixon

Field Summary
protected  boolean daemon
           
protected  JobQueue jobQ
          we synchronize access to this
protected  int maxLenJobQ
          If -1, length varies dynamically with no specific max; otherwise, rejects jobs if max queue length exceeded.
protected  int maxThreads
           
protected  java.lang.String name
           
protected  java.lang.ThreadGroup threadGroup
           
protected  ThreadList workers
          we synchronize access to this
 
Constructor Summary
ThreadPool(java.lang.String s, int maxP)
          Create a thread pool with a default queue size of 128 and using a default ThreadGroup
ThreadPool(java.lang.String s, int maxP, int maxJ, java.lang.ThreadGroup tg, boolean b)
          Create a thread pool, making up a default name if necessary.
ThreadPool(java.lang.String s, int maxP, java.lang.ThreadGroup tg)
          Create a thread group with a default queue size of 128
 
Method Summary
 boolean accept(java.lang.Runnable job)
          Accept a Runnable into the job queue.
 int getMaxLenJobQueue()
           
 int getMaxThreads()
           
 java.lang.String getName()
           
 int size()
           
 void stop()
          XXX Doesn't actually wait for worker threads to stop running.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

name

protected final java.lang.String name

threadGroup

protected final java.lang.ThreadGroup threadGroup

maxThreads

protected final int maxThreads

workers

protected final ThreadList workers
we synchronize access to this


maxLenJobQ

protected final int maxLenJobQ
If -1, length varies dynamically with no specific max; otherwise, rejects jobs if max queue length exceeded.


jobQ

protected final JobQueue jobQ
we synchronize access to this


daemon

protected final boolean daemon
Constructor Detail

ThreadPool

public ThreadPool(java.lang.String s,
                  int maxP)
Create a thread pool with a default queue size of 128 and using a default ThreadGroup


ThreadPool

public ThreadPool(java.lang.String s,
                  int maxP,
                  java.lang.ThreadGroup tg)
Create a thread group with a default queue size of 128


ThreadPool

public ThreadPool(java.lang.String s,
                  int maxP,
                  int maxJ,
                  java.lang.ThreadGroup tg,
                  boolean b)
Create a thread pool, making up a default name if necessary. XXX Probably should force a minimum maxP and maxJ

Parameters:
s - name of thread pool, base for thread names
maxP - maximum number of idle threads in pool
maxJ - maximum number of jobs in the queue
tg - thread group threads will belong to
Method Detail

getName

public java.lang.String getName()
Returns:
base name of this thread group

getMaxLenJobQueue

public int getMaxLenJobQueue()
Returns:
maximum number of jobs allowed in the queue

getMaxThreads

public int getMaxThreads()
Returns:
maximum number of threads in the pool

size

public int size()
Returns:
number of threads in the pool

accept

public boolean accept(java.lang.Runnable job)
Accept a Runnable into the job queue. If there are no idle workers, then if possible a new worker is created. The job is then queued, if there is enough room in the queue. There is no guarantee that the job causing a new worker to be created will be run in that new thread or even added to the queue.

Returns:
whether the job could be queued

stop

public void stop()
XXX Doesn't actually wait for worker threads to stop running. Preferred behavior: take a snapshot of all workers, tell each one to die, and then wait until isAlive() is false for each thread. Could use join() instead. Better: build a multi-level kill() that first checks isAlive(), then sets the dying flag using die() and waits a short time, and finally actually