org.mortbay.util
Class ThreadPool

java.lang.Object
  |
  +--org.mortbay.util.ThreadPool
All Implemented Interfaces:
LifeCycle
Direct Known Subclasses:
IO

public class ThreadPool
extends java.lang.Object
implements LifeCycle

A pool of threads.

Avoids the expense of thread creation by pooling threads after their run methods exit for reuse.

If the maximum pool size is reached, jobs wait for a free thread. By default there is no maximum pool size. Idle threads timeout and terminate until the minimum number of threads are running.

This implementation uses the run(Object) method to place a job on a queue, which is read by the getJob(timeout) method. Derived implementations may specialize getJob(timeout) to obtain jobs from other sources without queing overheads.

Version:
$Id: ThreadPool.java,v 1.1 2001/11/30 13:38:22 kobit Exp $
Author:
Juancarlo Añez , Greg Wilkins

Inner Class Summary
static class ThreadPool.PoolThread
          Pool Thread run class.
 
Field Summary
static int __nullLockChecks
          The number of times a null lock check should synchronize.
 
Constructor Summary
ThreadPool()
           
ThreadPool(java.lang.String name)
           
 
Method Summary
 void destroy()
          Destroy the ThreadPool.
 int getIdleThreads()
          Get the number of threads in the pool.
protected  java.lang.Object getJob(int idleTimeoutMs)
          Get a job.
 int getMaxIdleTimeMs()
          Get the maximum thread idle time.
 int getMaxSize()
          Deprecated. use getMaxThreads
 int getMaxStopTimeMs()
          Get the maximum thread stop time.
 int getMaxThreads()
          Set the maximum number of threads.
 int getMinSize()
          Deprecated. use getMinThreads
 int getMinThreads()
          Get the minimum number of threads.
 java.lang.String getName()
           
 int getSize()
          Deprecated. use getThreads
 java.lang.Class getThreadClass()
           
 int getThreads()
          Get the number of threads in the pool.
protected  void handle(java.lang.Object job)
          Handle a job.
 void initialize(java.lang.Object o)
          Not required.
 boolean isDestroyed()
           
 boolean isStarted()
          Is the pool running jobs.
 void join()
          Join the ThreadPool.
 void run(java.lang.Object job)
          Run job.
 void setMaxIdleTimeMs(int maxIdleTimeMs)
          Set the maximum thread idle time.
 void setMaxSize(int maxThreads)
          Deprecated. use setMaxThreads
 void setMaxStopTimeMs(int maxStopTimeMs)
          Set the maximum thread stop time.
 void setMaxThreads(int maxThreads)
          Set the maximum number of threads.
 void setMinSize(int minThreads)
          Deprecated. use setMinThreads
 void setMinThreads(int minThreads)
          Set the minimum number of threads.
 void setName(java.lang.String name)
           
 void setThreadClass(java.lang.Class threadClass)
          Set the Thread class.
 void start()
          Start the LifeCycle.
 void stop()
          Stop the ThreadPool.
protected  boolean stopJob(java.lang.Thread thread, java.lang.Object job)
          Stop a job.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

__nullLockChecks

public static int __nullLockChecks
The number of times a null lock check should synchronize.
Constructor Detail

ThreadPool

public ThreadPool()

ThreadPool

public ThreadPool(java.lang.String name)
Method Detail

getName

public java.lang.String getName()
Returns:
 

setName

public void setName(java.lang.String name)
Parameters:
name -  

setThreadClass

public void setThreadClass(java.lang.Class threadClass)
                    throws java.lang.IllegalStateException
Set the Thread class. Sets the class used for threads in the thread pool. The class must have a constractor taking a Runnable.
Parameters:
threadClas - The class
Throws:
java.lang.IllegalStateException - If the pool has already been started.

getThreadClass

public java.lang.Class getThreadClass()

handle

protected void handle(java.lang.Object job)
               throws java.lang.InterruptedException
Handle a job. Unless the job is an instance of Runnable, then this method must be specialized by a derived class.
Parameters:
job - The Job to handle. If it implements Runnable, this implementation calls run().

isStarted

public boolean isStarted()
Is the pool running jobs.
Specified by:
isStarted in interface LifeCycle
Returns:
True if start() has been called.

isDestroyed

public boolean isDestroyed()
Specified by:
isDestroyed in interface LifeCycle
Following copied from interface: org.mortbay.util.LifeCycle
Returns:
True if the LifeCycle has been destroyed.

getThreads

public int getThreads()
Get the number of threads in the pool.
Returns:
Number of threads

getIdleThreads

public int getIdleThreads()
Get the number of threads in the pool.
Returns:
Number of threads

getMinThreads

public int getMinThreads()
Get the minimum number of threads.
Returns:
minimum number of threads.

setMinThreads

public void setMinThreads(int minThreads)
Set the minimum number of threads.
Parameters:
minThreads - minimum number of threads

getMaxThreads

public int getMaxThreads()
Set the maximum number of threads.
Returns:
maximum number of threads.

setMaxThreads

public void setMaxThreads(int maxThreads)
Set the maximum number of threads.
Parameters:
maxThreads - maximum number of threads.

getMaxIdleTimeMs

public int getMaxIdleTimeMs()
Get the maximum thread idle time.
Returns:
Max idle time in ms.

setMaxIdleTimeMs

public void setMaxIdleTimeMs(int maxIdleTimeMs)
Set the maximum thread idle time. Threads that are idle for longer than this period may be destroyed.
Parameters:
maxIdleTimeMs - Max idle time in ms.

getMaxStopTimeMs

public int getMaxStopTimeMs()
Get the maximum thread stop time. Threads that do not stop within this time are interrupted and then discarded. If <=0 the max idle time is used instead.
Returns:
Max stop time in ms.

setMaxStopTimeMs

public void setMaxStopTimeMs(int maxStopTimeMs)
Set the maximum thread stop time. Threads that do not stop within this time are interrupted and then discarded. If <=0 the max idle time is used instead.
Parameters:
maxIdleTimeMs - Max stop time in ms.

initialize

public void initialize(java.lang.Object o)
Not required. Calls destroy().
Parameters:
o - ignored.

start

public void start()
           throws java.lang.Exception
Description copied from interface: LifeCycle
Start the LifeCycle.
Specified by:
start in interface LifeCycle
Following copied from interface: org.mortbay.util.LifeCycle
Throws:
java.lang.Exception - An arbitrary exception may be thrown.

stop

public void stop()
          throws java.lang.InterruptedException
Stop the ThreadPool. New jobs are no longer accepted, idle threads are interrupted
Specified by:
stop in interface LifeCycle

stopJob

protected boolean stopJob(java.lang.Thread thread,
                          java.lang.Object job)
Stop a job. Called by stop() to encourage a active job to stop. Implementations of this method are under no obligation to interrupt active work and the default implementation waits for the job to complete. The default implementation will return true only if the job is being handled by an inactive PoolThread.
Parameters:
thread - The Thread running the job
job - The job, or null if it cannot be determined
Returns:
True if the job can be considered stopped.

destroy

public void destroy()
Destroy the ThreadPool. All threads are interrupted and if they do not terminate after a short delay, they are stopped.
Specified by:
destroy in interface LifeCycle

join

public final void join()
                throws java.lang.InterruptedException
Join the ThreadPool. Wait for all threads to complete.
Throws:
java.lang.InterruptedException -  

getJob

protected java.lang.Object getJob(int idleTimeoutMs)
                           throws java.lang.InterruptedException,
                                  java.io.InterruptedIOException
Get a job. This method is called by the ThreadPool to get jobs. The call blocks until a job is available. The default implementation removes jobs from the BlockingQueue used by the run(Object) method. Derived implementations of ThreadPool may specialize this method to obtain jobs from other sources.
Parameters:
idleTimeoutMs - The timout to wait for a job.
Returns:
Job or null if no job available after timeout.
Throws:
java.lang.InterruptedException -  
java.io.InterruptedIOException -  

run

public void run(java.lang.Object job)
         throws java.lang.InterruptedException
Run job. Give a job to the pool. The job is passed via a BlockingQueue with the same capacity as the ThreadPool.
Parameters:
job. - If the job is derived from Runnable, the run method is called, otherwise it is passed as the argument to the handle method.

getSize

public int getSize()
Deprecated. use getThreads

Get the number of threads in the pool.
Returns:
Number of threads

getMinSize

public int getMinSize()
Deprecated. use getMinThreads

Get the minimum number of threads.
Returns:
minimum number of threads.

setMinSize

public void setMinSize(int minThreads)
Deprecated. use setMinThreads

Set the minimum number of threads.
Parameters:
minThreads - minimum number of threads

getMaxSize

public int getMaxSize()
Deprecated. use getMaxThreads

Set the maximum number of threads.
Returns:
maximum number of threads.

setMaxSize

public void setMaxSize(int maxThreads)
Deprecated. use setMaxThreads

Set the maximum number of threads.
Parameters:
maxThreads - maximum number of threads.


Copyright © GNU, wttools developers Team.