Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.mortbay.util
Class ThreadPool  view ThreadPool download ThreadPool.java

java.lang.Object
  extended byorg.mortbay.util.ThreadPool
All Implemented Interfaces:
LifeCycle, java.io.Serializable
Direct Known Subclasses:
IO, TestHarness.TestThreadPool, ThreadedServer

public class ThreadPool
extends java.lang.Object
implements LifeCycle, java.io.Serializable

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.31 2003/11/16 11:54:30 gregwilkins Exp $

Nested Class Summary
static class ThreadPool.PoolThread
          Pool Thread class.
 
Field Summary
static java.lang.String __DAEMON
           
static java.lang.String __PRIORITY
           
private  java.lang.Object _join
           
private  java.lang.String _name
           
private  Pool _pool
           
private  boolean _started
           
(package private) static org.apache.commons.logging.Log log
           
 
Constructor Summary
ThreadPool()
           
 
Method Summary
 int getIdleThreads()
          Get the number of idle threads in the pool.
 int getMaxIdleTimeMs()
          Get the maximum thread idle time.
 int getMaxThreads()
          Set the maximum number of threads.
 int getMinThreads()
          Get the minimum number of threads.
 java.lang.String getName()
           
 java.lang.String getPoolName()
           
 int getThreads()
          Get the number of threads in the pool.
 int getThreadsPriority()
          Get the priority of the pool threads.
protected  void handle(java.lang.Object job)
          Handle a job.
 boolean isDaemon()
          Delegated to the named or anonymous Pool.
 boolean isStarted()
          Is the pool running jobs.
 void join()
           
 void run(java.lang.Object job)
          Run job.
 void setDaemon(boolean daemon)
          Delegated to the named or anonymous Pool.
 void setMaxIdleTimeMs(int maxIdleTimeMs)
          Set the maximum thread idle time.
 void setMaxStopTimeMs(int ms)
          Deprecated. maxIdleTime is used instead.
 void setMaxThreads(int maxThreads)
          Set the maximum number of threads.
 void setMinThreads(int minThreads)
          Set the minimum number of threads.
 void setName(java.lang.String name)
           
 void setPoolName(java.lang.String name)
          Set the Pool name.
 void setThreadsPriority(int priority)
          Set the priority of the pool threads.
 void shrink()
           
 void start()
          Start the LifeCycle.
 void stop()
          Stop the ThreadPool.
protected  void 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

log

static org.apache.commons.logging.Log log

__DAEMON

public static final java.lang.String __DAEMON
See Also:
Constant Field Values

__PRIORITY

public static final java.lang.String __PRIORITY
See Also:
Constant Field Values

_name

private java.lang.String _name

_pool

private Pool _pool

_join

private java.lang.Object _join

_started

private transient boolean _started
Constructor Detail

ThreadPool

public ThreadPool()
Method Detail

getName

public java.lang.String getName()

setName

public void setName(java.lang.String name)

getPoolName

public java.lang.String getPoolName()

setPoolName

public void setPoolName(java.lang.String name)
Set the Pool name. All ThreadPool instances with the same Pool name will share the same Pool instance. Thus they will share the same max, min and available Threads. The field values of the first ThreadPool to call setPoolName with a specific name are used for the named Pool. Subsequent ThreadPools that join the name pool will loose their private values.


isDaemon

public boolean isDaemon()
Delegated to the named or anonymous Pool.


setDaemon

public void setDaemon(boolean daemon)
Delegated to the named or anonymous Pool.


isStarted

public boolean isStarted()
Is the pool running jobs.

Specified by:
isStarted in interface LifeCycle

getThreads

public int getThreads()
Get the number of threads in the pool. Delegated to the named or anonymous Pool.


getIdleThreads

public int getIdleThreads()
Get the number of idle threads in the pool. Delegated to the named or anonymous Pool.


getMinThreads

public int getMinThreads()
Get the minimum number of threads. Delegated to the named or anonymous Pool.


setMinThreads

public void setMinThreads(int minThreads)
Set the minimum number of threads. Delegated to the named or anonymous Pool.


getMaxThreads

public int getMaxThreads()
Set the maximum number of threads. Delegated to the named or anonymous Pool.


setMaxThreads

public void setMaxThreads(int maxThreads)
Set the maximum number of threads. Delegated to the named or anonymous Pool.


getMaxIdleTimeMs

public int getMaxIdleTimeMs()
Get the maximum thread idle time. Delegated to the named or anonymous Pool.


setMaxIdleTimeMs

public void setMaxIdleTimeMs(int maxIdleTimeMs)
Set the maximum thread idle time. Threads that are idle for longer than this period may be stopped. Delegated to the named or anonymous Pool.


getThreadsPriority

public int getThreadsPriority()
Get the priority of the pool threads.


setThreadsPriority

public void setThreadsPriority(int priority)
Set the priority of the pool threads.


setMaxStopTimeMs

public void setMaxStopTimeMs(int ms)
Deprecated. maxIdleTime is used instead.

Set Max Read Time.


start

public void start()
           throws java.lang.Exception
Description copied from interface: LifeCycle
Start the LifeCycle.

Specified by:
start in interface LifeCycle

stop

public void stop()
          throws java.lang.InterruptedException
Stop the ThreadPool. New jobs are no longer accepted,idle threads are interrupted and stopJob is called on active threads. The method then waits min(getMaxStopTimeMs(),getMaxIdleTimeMs()), for all jobs to stop, at which time killJob is called.

Specified by:
stop in interface LifeCycle

join

public void join()

shrink

public void shrink()
            throws java.lang.InterruptedException

run

public void run(java.lang.Object job)
         throws java.lang.InterruptedException
Run job. Give a job to the pool.


handle

protected void handle(java.lang.Object job)
               throws java.lang.InterruptedException
Handle a job. Called by the allocated thread to handle a job. If the job is a Runnable, it's run method is called. Otherwise this method needs to be specialized by a derived class to provide specific handling.


stopJob

protected void stopJob(java.lang.Thread thread,
                       java.lang.Object job)
Stop a Job. This method is called by the Pool if a job needs to be stopped. The default implementation does nothing and should be extended by a derived thread pool class if special action is required.