com.mischiefbox.pollserve
Class ThreadPool

java.lang.Object
  |
  +--com.mischiefbox.pollserve.ThreadPool
All Implemented Interfaces:
Runnable

public class ThreadPool
extends Object
implements Runnable

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.

Version:
$Id$
Author:
Chris Jones

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

SLEEP_MILLIS

protected static int SLEEP_MILLIS
Time between thread pool checks. The number of milliseconds between each check of the thread pool.

fIdleRatio

protected float fIdleRatio
Idle thread ratio. The ratio of in-use threads to idle threads that should be maintained. Idle threads will not be collected until their minimum idle lifetime has been exceeded. New threads will be created when the ratio is too high.

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.


iIdleLifetime

protected int iIdleLifetime
The maximum idle thread lifetime. The number of concurrent seconds that a thread may be idle before being collected. If set to 0, the idle thread will never be collected.

iMaxThreads

protected int iMaxThreads
The maximum number of threads in the thread pool.

tManager

protected Thread tManager
The management thread.

qThreads

protected Queue qThreads
The available thread pool. Implemented as a queue (which will permit round-robin access to threads in the pool).

vThreads

protected Vector vThreads
The complete thread pool.

bManaging

protected boolean bManaging
Indicates if this is managing the thread pool.
Constructor Detail

ThreadPool

public ThreadPool(int iMaxThreads)
Create a new thread pool.
Parameters:
iMaxThreads - the maximum number of threads in the thread pool.

ThreadPool

public ThreadPool(int iMaxThreads,
                  float fIdleRatio)
Create a new thread pool and specify the idle ratio.
Parameters:
iMaxThreads - the maximum number of threads in the thread pool.
fIdleRatio - the ratio of active threads to idle threads.

ThreadPool

public 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.
Parameters:
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

shutdown

public void shutdown()
Shut down the thread pool. Turn off thread management. Tell each thread in the pool to shut down as soon as possible.

Synchronized.


registerProcessorThread

public void registerProcessorThread(ProcessorThread pt)
Register a newly created processor thread with the pool.

Synchronized access to the pool.

Parameters:
pt - the new processor thread.

unregisterProcessorThread

public void unregisterProcessorThread(ProcessorThread pt)
Unregister a processor thread from the pool.

Synchronized access to the pool.

Parameters:
pt - the processor thread to remove.

nextFree

public ProcessorThread nextFree()
Get the next free thread from the pool. Blocks until the thread is available.
Returns:
the next free processor thread.

nowFree

public void nowFree(ProcessorThread pt)
Set a processor thread as free (idle).

Should only be called by the ProcessorThread.

Parameters:
pt - the idle processor thread.

getFree

public ProcessorThread getFree()
                        throws NoSuchElementException
Get the next free thread from the pool. Non-blocking method.
Returns:
the next free processor thread.
Throws:
NoSuchElementException - when there is no free processor thread.

run

public void run()
Manage the processor thread pool.

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.

Specified by:
run in interface Runnable


Copyright © 2001 by Christopher R. Jones. All Rights Reserved.