Top Description Inners Fields Constructors Methods
org.mortbay.thread

public Class QueuedThreadPool

extends AbstractLifeCycle
implements Serializable, ThreadPool
Class Inheritance
All Implemented Interfaces
org.mortbay.thread.ThreadPool, java.io.Serializable
Imports
java.io.Serializable, java.util.ArrayList, .HashSet, .Iterator, .List, .Set, org.mortbay.component.AbstractLifeCycle, org.mortbay.log.Log

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

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
public class
QueuedThreadPool.PoolThread

Pool Thread class.

Field Summary

Modifier and TypeField and Description
private static int
private boolean
private int
private List<E>
private Runnable[]
private final Object
private long
private final Object
private int
private int
private int
private int
private int
private int
private String
private int
private int
private int
private int
private int
private Set<E>
private final Object
private boolean
Inherited from org.mortbay.component.AbstractLifeCycle:
_listeners

Constructor Summary

AccessConstructor and Description
public
public
QueuedThreadPool(int maxThreads)

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true
dispatch
(Runnable job)

Implements org.mortbay.thread.ThreadPool.dispatch.

Run job.
protected void
protected void
doStop()

Overrides org.mortbay.component.AbstractLifeCycle.doStop.

Stop the BoundedThreadPool.
public int

Returns:

Number of threads
getIdleThreads
()

Implements org.mortbay.thread.ThreadPool.getIdleThreads.

Get the number of idle threads in the pool.
public int

Returns:

low resource threads threshhold
getLowThreads
()

public int

Returns:

Max idle time in ms.
getMaxIdleTimeMs
()

Get the maximum thread idle time.

public int

Returns:

maximum queue size
getMaxQueued
()

public int

Returns:

maximum total time that stop() will wait for threads to die.
getMaxStopTimeMs
()

public int

Returns:

maximum number of threads.
getMaxThreads
()

Set the maximum number of threads.

public int

Returns:

minimum number of threads.
getMinThreads
()

Get the minimum number of threads.

public String

Returns:

The name of the BoundedThreadPool.
getName
()

public int
public int

Returns:

the spawnOrShrinkAt The number of queued jobs (or idle threads) needed before the thread pool is grown (or shrunk)
getSpawnOrShrinkAt
()

public int

Returns:

Number of threads
getThreads
()

Implements org.mortbay.thread.ThreadPool.getThreads.

Get the number of threads in the pool.
public int

Returns:

the priority of the pool threads.
getThreadsPriority
()

Get the priority of the pool threads.

public boolean
isDaemon()

Delegated to the named or anonymous Pool.

public boolean
public void
join()

Implements org.mortbay.thread.ThreadPool.join.

Blocks until the thread pool is stopped.
protected void
public void
setDaemon(boolean daemon)

Delegated to the named or anonymous Pool.

public void
setLowThreads(int
low resource threads threshhold
lowThreads
)

public void
setMaxIdleTimeMs(int
Max idle time in ms.
maxIdleTimeMs
)

Set the maximum thread idle time.

public void
setMaxStopTimeMs(int
maximum total time that stop() will wait for threads to die.
stopTimeMs
)

public void
setMaxThreads(int
maximum number of threads.
maxThreads
)

Set the maximum number of threads.

public void
setMinThreads(int
minimum number of threads
minThreads
)

Set the minimum number of threads.

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

public void
setSpawnOrShrinkAt(int
The number of queued jobs (or idle threads) needed before the thread pool is grown (or shrunk)
spawnOrShrinkAt
)

public void
setThreadsPriority(int
the new thread priority.
priority
)

Set the priority of the pool threads.

protected void
stopJob(Thread
The thread allocated to the job, or null if no thread allocated.
thread
,
Object
The job object passed to run.
job
)

Stop a Job.

Inherited from org.mortbay.component.AbstractLifeCycle:
addLifeCycleListenerisFailedisRunningisStartedisStartingisStoppedisStoppingremoveLifeCycleListenerstartstop

Field Detail

__idback to summary
private static int __id
_daemonback to summary
private boolean _daemon
_idback to summary
private int _id
_idleback to summary
private List<E> _idle
_jobsback to summary
private Runnable[] _jobs
_joinLockback to summary
private final Object _joinLock
_lastShrinkback to summary
private long _lastShrink
_lockback to summary
private final Object _lock

Hides org.mortbay.component.AbstractLifeCycle._lock.

_lowThreadsback to summary
private int _lowThreads
_maxIdleTimeMsback to summary
private int _maxIdleTimeMs
_maxQueuedback to summary
private int _maxQueued
_maxStopTimeMsback to summary
private int _maxStopTimeMs
_maxThreadsback to summary
private int _maxThreads
_minThreadsback to summary
private int _minThreads
_nameback to summary
private String _name
_nextJobback to summary
private int _nextJob
_nextJobSlotback to summary
private int _nextJobSlot
_priorityback to summary
private int _priority
_queuedback to summary
private int _queued
_spawnOrShrinkAtback to summary
private int _spawnOrShrinkAt
_threadsback to summary
private Set<E> _threads
_threadsLockback to summary
private final Object _threadsLock
_warnedback to summary
private boolean _warned

Constructor Detail

QueuedThreadPoolback to summary
public QueuedThreadPool()
QueuedThreadPoolback to summary
public QueuedThreadPool(int maxThreads)

Method Detail

dispatchback to summary
public boolean dispatch(Runnable job)

Implements org.mortbay.thread.ThreadPool.dispatch.

Run job.

Returns:boolean

true

doStartback to summary
protected void doStart() throws Exception

Overrides org.mortbay.component.AbstractLifeCycle.doStart.

doStopback to summary
protected void doStop() throws Exception

Overrides org.mortbay.component.AbstractLifeCycle.doStop.

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.

getIdleThreadsback to summary
public int getIdleThreads()

Implements org.mortbay.thread.ThreadPool.getIdleThreads.

Get the number of idle threads in the pool.

Returns:int

Number of threads

See Also
getThreads
getLowThreadsback to summary
public int getLowThreads()
Returns:int

low resource threads threshhold

getMaxIdleTimeMsback to summary
public int getMaxIdleTimeMs()

Get the maximum thread idle time. Delegated to the named or anonymous Pool.

Returns:int

Max idle time in ms.

See Also
setMaxIdleTimeMs
getMaxQueuedback to summary
public int getMaxQueued()
Returns:int

maximum queue size

getMaxStopTimeMsback to summary
public int getMaxStopTimeMs()
Returns:int

maximum total time that stop() will wait for threads to die.

getMaxThreadsback to summary
public int getMaxThreads()

Set the maximum number of threads. Delegated to the named or anonymous Pool.

Returns:int

maximum number of threads.

See Also
setMaxThreads
getMinThreadsback to summary
public int getMinThreads()

Get the minimum number of threads. Delegated to the named or anonymous Pool.

Returns:int

minimum number of threads.

See Also
setMinThreads
getNameback to summary
public String getName()
Returns:String

The name of the BoundedThreadPool.

getQueueSizeback to summary
public int getQueueSize()
getSpawnOrShrinkAtback to summary
public int getSpawnOrShrinkAt()
Returns:int

the spawnOrShrinkAt The number of queued jobs (or idle threads) needed before the thread pool is grown (or shrunk)

getThreadsback to summary
public int getThreads()

Implements org.mortbay.thread.ThreadPool.getThreads.

Get the number of threads in the pool.

Returns:int

Number of threads

See Also
getIdleThreads
getThreadsPriorityback to summary
public int getThreadsPriority()

Get the priority of the pool threads.

Returns:int

the priority of the pool threads.

isDaemonback to summary
public boolean isDaemon()

Delegated to the named or anonymous Pool.

isLowOnThreadsback to summary
public boolean isLowOnThreads()

Implements org.mortbay.thread.ThreadPool.isLowOnThreads.

Returns:boolean

Doc from org.mortbay.thread.ThreadPool.isLowOnThreads.

True if the pool is low on threads

joinback to summary
public void join() throws InterruptedException

Implements org.mortbay.thread.ThreadPool.join.

Doc from org.mortbay.thread.ThreadPool.join.

Blocks until the thread pool is stopped.

newThreadback to summary
protected void newThread()
setDaemonback to summary
public void setDaemon(boolean daemon)

Delegated to the named or anonymous Pool.

setLowThreadsback to summary
public void setLowThreads(int lowThreads)
Parameters
lowThreads:int

low resource threads threshhold

setMaxIdleTimeMsback to summary
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:int

Max idle time in ms.

See Also
getMaxIdleTimeMs
setMaxStopTimeMsback to summary
public void setMaxStopTimeMs(int stopTimeMs)
Parameters
stopTimeMs:int

maximum total time that stop() will wait for threads to die.

setMaxThreadsback to summary
public void setMaxThreads(int maxThreads)

Set the maximum number of threads. Delegated to the named or anonymous Pool.

Parameters
maxThreads:int

maximum number of threads.

See Also
getMaxThreads
setMinThreadsback to summary
public void setMinThreads(int minThreads)

Set the minimum number of threads. Delegated to the named or anonymous Pool.

Parameters
minThreads:int

minimum number of threads

See Also
getMinThreads
setNameback to summary
public void setName(String name)
Parameters
name:String

Name of the BoundedThreadPool to use when naming Threads.

setSpawnOrShrinkAtback to summary
public void setSpawnOrShrinkAt(int spawnOrShrinkAt)
Parameters
spawnOrShrinkAt:int

The number of queued jobs (or idle threads) needed before the thread pool is grown (or shrunk)

setThreadsPriorityback to summary
public void setThreadsPriority(int priority)

Set the priority of the pool threads.

Parameters
priority:int

the new thread priority.

stopJobback to summary
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:Thread

The thread allocated to the job, or null if no thread allocated.

job:Object

The job object passed to run.

org.mortbay.thread back to summary

private Class QueuedThreadPool.Lock

extends Object
Class Inheritance

Constructor Summary

AccessConstructor and Description
private
Lock()

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Lockback to summary
private Lock()
org.mortbay.thread back to summary

public Class QueuedThreadPool.PoolThread

extends Thread
Class Inheritance

Pool Thread class. The PoolThread allows the threads job to be retrieved and active status to be indicated.

Field Summary

Modifier and TypeField and Description
pack-priv Runnable
Inherited from java.lang.Thread:
MAX_PRIORITYMIN_PRIORITYNORM_PRIORITY

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv void
public void
run()

Overrides java.lang.Thread.run.

Implements java.lang.Runnable.run.

BoundedThreadPool run.
Inherited from java.lang.Thread:
activeCountcheckAccessclonecountStackFramescurrentThreaddumpStackenumerategetAllStackTracesgetContextClassLoadergetDefaultUncaughtExceptionHandlergetIdgetNamegetPrioritygetStackTracegetStategetThreadGroupgetUncaughtExceptionHandlerholdsLockinterruptinterruptedisAliveisDaemonisInterruptedisVirtualjoinjoinjoinjoinofPlatformofVirtualonSpinWaitresumesetContextClassLoadersetDaemonsetDefaultUncaughtExceptionHandlersetNamesetPrioritysetUncaughtExceptionHandlersleepsleepsleepstartstartVirtualThreadstopsuspendthreadIdtoStringyield

Field Detail

_jobback to summary
pack-priv Runnable _job

Constructor Detail

PoolThreadback to summary
pack-priv PoolThread()

Method Detail

dispatchback to summary
pack-priv void dispatch(Runnable job)
runback to summary
public void run()

Overrides java.lang.Thread.run.

Implements java.lang.Runnable.run.

BoundedThreadPool run. Loop getting jobs and handling them until idle or stopped.