org.mortbay.thread
Class BoundedThreadPool

java.lang.Object
  extended byorg.mortbay.component.AbstractLifeCycle
      extended byorg.mortbay.thread.BoundedThreadPool
All Implemented Interfaces:
LifeCycle, Serializable, ThreadPool
Direct Known Subclasses:
IO

public class BoundedThreadPool
extends AbstractLifeCycle
implements Serializable, ThreadPool

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.

Author:
Greg Wilkins , Juancarlo Anez
See Also:
Serialized Form

Nested Class Summary
 class BoundedThreadPool.PoolThread
          Pool Thread class.
 
Constructor Summary
BoundedThreadPool()
           
 
Method Summary
 boolean dispatch(Runnable job)
          Run job.
protected  void doStart()
           
protected  void doStop()
          Stop the BoundedThreadPool.
 int getIdleThreads()
          Get the number of idle threads in the pool.
 int getLowThreads()
           
 int getMaxIdleTimeMs()
          Get the maximum thread idle time.
 int getMaxThreads()
          Set the maximum number of threads.
 int getMinThreads()
          Get the minimum number of threads.
 String getName()
           
 int getThreads()
          Get the number of threads in the pool.
 int getThreadsPriority()
          Get the priority of the pool threads.
 boolean isDaemon()
          Delegated to the named or anonymous Pool.
 boolean isLowOnThreads()
           
 void join()
          Blocks until the thread pool is stopped.
protected  void newThread()
           
 void setDaemon(boolean daemon)
          Delegated to the named or anonymous Pool.
 void setLowThreads(int lowThreads)
           
 void setMaxIdleTimeMs(int maxIdleTimeMs)
          Set the maximum thread idle time.
 void setMaxThreads(int maxThreads)
          Set the maximum number of threads.
 void setMinThreads(int minThreads)
          Set the minimum number of threads.
 void setName(String name)
           
 void setThreadsPriority(int priority)
          Set the priority of the pool threads.
protected  void stopJob(Thread thread, Object job)
          Stop a Job.
 
Methods inherited from class org.mortbay.component.AbstractLifeCycle
isFailed, isRunning, isStarted, isStarting, isStopping, start, stop
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BoundedThreadPool

public BoundedThreadPool()
Method Detail

getIdleThreads

public int getIdleThreads()
Get the number of idle threads in the pool.

Specified by:
getIdleThreads in interface ThreadPool
Returns:
Number of threads
See Also:
getThreads()

getLowThreads

public int getLowThreads()
Returns:
low resource threads threshhold

setLowThreads

public void setLowThreads(int lowThreads)
Parameters:
lowThreads - low resource threads threshhold

isLowOnThreads

public boolean isLowOnThreads()
Specified by:
isLowOnThreads in interface ThreadPool
Returns:
True if the pool is low on threads

getMaxIdleTimeMs

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

Returns:
Max idle time in ms.
See Also:
setMaxIdleTimeMs(int)

getMaxThreads

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

Returns:
maximum number of threads.
See Also:
setMaxThreads(int)

getMinThreads

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

Returns:
minimum number of threads.
See Also:
setMinThreads(int)

getName

public String getName()
Returns:
The name of the BoundedThreadPool.

getThreads

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

Specified by:
getThreads in interface ThreadPool
Returns:
Number of threads
See Also:
getIdleThreads()

getThreadsPriority

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

Returns:
the priority of the pool threads.

isDaemon

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


join

public void join()
          throws InterruptedException
Description copied from interface: ThreadPool
Blocks until the thread pool is stopped.

Specified by:
join in interface ThreadPool
Throws:
InterruptedException

newThread

protected void newThread()

dispatch

public boolean dispatch(Runnable job)
Run job.

Specified by:
dispatch in interface ThreadPool
Returns:
true if the job was given to a thread, false if no thread was available.

setDaemon

public void setDaemon(boolean daemon)
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.

Parameters:
maxIdleTimeMs - Max idle time in ms.
See Also:
getMaxIdleTimeMs()

setMaxThreads

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

Parameters:
maxThreads - maximum number of threads.
See Also:
getMaxThreads()

setMinThreads

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

Parameters:
minThreads - minimum number of threads
See Also:
getMinThreads()

setName

public void setName(String name)
Parameters:
name - Name of the BoundedThreadPool to use when naming Threads.

setThreadsPriority

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

Parameters:
priority - the new thread priority.

doStart

protected void doStart()
                throws Exception
Overrides:
doStart in class AbstractLifeCycle
Throws:
Exception

doStop

protected void doStop()
               throws Exception
Stop the BoundedThreadPool. 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.

Overrides:
doStop in class AbstractLifeCycle
Throws:
Exception

stopJob

protected void stopJob(Thread thread,
                       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.

Parameters:
thread - The thread allocated to the job, or null if no thread allocated.
job - The job object passed to run.