org.mortbay.thread
Class BoundedThreadPool

java.lang.Object
  extended by org.mortbay.component.AbstractLifeCycle
      extended by org.mortbay.thread.BoundedThreadPool
All Implemented Interfaces:
Serializable, LifeCycle, 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.
 
Field Summary
private static int __id
           
private  List _blocked
           
private  int _blockMs
           
private  boolean _daemon
           
private  int _id
           
private  List _idle
           
private  String _joinLock
           
private  String _lock
           
private  int _maxIdleTimeMs
           
private  int _maxThreads
           
private  int _minThreads
           
private  String _name
           
(package private)  int _priority
           
private  Set _threads
           
private  boolean _warned
           
private static long serialVersionUID
           
 
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 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.
 void join()
           
protected  void newThread()
           
 void setDaemon(boolean daemon)
          Delegated to the named or anonymous Pool.
 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
 
Methods inherited from interface org.mortbay.component.LifeCycle
isFailed, isRunning, isStarted, isStarting, isStopping, start, stop
 

Field Detail

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

__id

private static int __id

_blocked

private transient List _blocked

_blockMs

private int _blockMs

_daemon

private boolean _daemon

_id

private transient int _id

_lock

private final String _lock
See Also:
Constant Field Values

_joinLock

private final String _joinLock
See Also:
Constant Field Values

_maxIdleTimeMs

private int _maxIdleTimeMs

_maxThreads

private int _maxThreads

_minThreads

private int _minThreads

_name

private String _name

_priority

int _priority

_threads

private Set _threads

_idle

private List _idle

_warned

private boolean _warned
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()

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
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.