Class QueuedThreadPool
- java.lang.Object
-
- org.eclipse.jetty.util.component.AbstractLifeCycle
-
- org.eclipse.jetty.util.component.ContainerLifeCycle
-
- org.eclipse.jetty.util.thread.QueuedThreadPool
-
- All Implemented Interfaces:
java.util.concurrent.Executor,java.util.concurrent.ThreadFactory,Container,Destroyable,Dumpable,Dumpable.DumpableContainer,LifeCycle,ThreadPool,ThreadPool.SizedThreadPool,TryExecutor
- Direct Known Subclasses:
MonitoredQueuedThreadPool
@ManagedObject("A thread pool") public class QueuedThreadPool extends ContainerLifeCycle implements java.util.concurrent.ThreadFactory, ThreadPool.SizedThreadPool, Dumpable, TryExecutor
A thread pool with a queue of jobs to execute.
Jetty components that need threads (such as network acceptors and selector) may lease threads from this thread pool using a
ThreadPoolBudget; these threads are "active" from the point of view of the thread pool, but not available to run transient jobs such as processing an HTTP request or a WebSocket frame.QueuedThreadPool has a
ReservedThreadExecutorwhich leases threads from this pool, but makes them available as if they are "idle" threads.QueuedThreadPool therefore has the following fundamental values:
threads: the current number of threads. These threads may execute a job (either internal or transient), or may be ready to run (either idle or reserved). This number may grow or shrink as the thread pool grows or shrinks.readyThreads: the current number of threads that are ready to run transient jobs. This number may grow or shrink as the thread pool grows or shrinks.leasedThreads: the number of threads that run internal jobs. This number is typically constant after this thread pool isstarted.
Given the definitions above, the most interesting definitions are:
threads=readyThreads+leasedThreads+utilizedThreads- readyThreads =
idleThreads+availableReservedThreads maxAvailableThreads=maxThreads- leasedThreadsutilizationRate= utilizedThreads / maxAvailableThreads
Other definitions, typically less interesting because they take into account threads that execute internal jobs, or because they don't take into account available reserved threads (that are essentially ready to execute transient jobs), are:
busyThreads= utilizedThreads + leasedThreadsgetIdleThreads()idleThreads} = readyThreads - availableReservedThreads
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classQueuedThreadPool.Runner-
Nested classes/interfaces inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
AbstractLifeCycle.AbstractLifeCycleListener
-
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.Container
Container.InheritedListener, Container.Listener
-
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.Dumpable
Dumpable.DumpableContainer
-
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.LifeCycle
LifeCycle.Listener
-
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.thread.ThreadPool
ThreadPool.SizedThreadPool
-
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.thread.TryExecutor
TryExecutor.NoTryExecutor
-
-
Field Summary
Fields Modifier and Type Field Description private ThreadPoolBudget_budgetprivate AtomicBiInteger_countsEncodes thread counts: HiTotal thread count or Integer.MIN_VALUE if the pool is stopping LoNet idle threads == idle threads - job queue size.private boolean_daemonprivate boolean_detailedDumpprivate int_idleTimeoutprivate java.util.concurrent.BlockingQueue<java.lang.Runnable>_jobsprivate java.lang.Object_joinLockprivate java.util.concurrent.atomic.AtomicLong_lastShrinkprivate int_lowThreadsThresholdprivate int_maxThreadsprivate int_minThreadsprivate java.lang.String_nameprivate int_priorityprivate int_reservedThreadsprivate java.lang.Runnable_runnableprivate java.util.concurrent.ThreadFactory_threadFactoryprivate java.lang.ThreadGroup_threadGroupprivate java.util.Set<java.lang.Thread>_threadsprivate TryExecutor_tryExecutorprivate static LoggerLOGprivate static java.lang.RunnableNOOP-
Fields inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
FAILED, RUNNING, STARTED, STARTING, STOPPED, STOPPING
-
Fields inherited from interface org.eclipse.jetty.util.thread.TryExecutor
NO_TRY
-
-
Constructor Summary
Constructors Constructor Description QueuedThreadPool()QueuedThreadPool(int maxThreads)QueuedThreadPool(int maxThreads, int minThreads)QueuedThreadPool(int maxThreads, int minThreads, int idleTimeout)QueuedThreadPool(int maxThreads, int minThreads, int idleTimeout, int reservedThreads, java.util.concurrent.BlockingQueue<java.lang.Runnable> queue, java.lang.ThreadGroup threadGroup)QueuedThreadPool(int maxThreads, int minThreads, int idleTimeout, int reservedThreads, java.util.concurrent.BlockingQueue<java.lang.Runnable> queue, java.lang.ThreadGroup threadGroup, java.util.concurrent.ThreadFactory threadFactory)QueuedThreadPool(int maxThreads, int minThreads, int idleTimeout, java.util.concurrent.BlockingQueue<java.lang.Runnable> queue)QueuedThreadPool(int maxThreads, int minThreads, int idleTimeout, java.util.concurrent.BlockingQueue<java.lang.Runnable> queue, java.lang.ThreadGroup threadGroup)QueuedThreadPool(int maxThreads, int minThreads, java.util.concurrent.BlockingQueue<java.lang.Runnable> queue)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description private booleanaddCounts(int deltaThreads, int deltaIdle)protected voiddoStart()Starts the managed lifecycle beans in the order they were added.protected voiddoStop()Stops the managed lifecycle beans in the reverse order they were added.voiddump(java.lang.Appendable out, java.lang.String indent)Dump this object (and children) into an Appendable using the provided indent after any new lines.java.lang.StringdumpThread(long id)private voidensureThreads()voidexecute(java.lang.Runnable job)intgetAvailableReservedThreads()intgetBusyThreads()The number of threads executing internal and transient jobs.private java.lang.StringgetCompressedStackTag(java.lang.StackTraceElement[] trace)intgetIdleThreads()The number of idle threads, but without including reserved threads.intgetIdleTimeout()intgetLeasedThreads()The fundamental value that represents the number of threads that are leased to internal components, and therefore cannot be used to execute transient jobs.intgetLowThreadsThreshold()intgetMaxAvailableThreads()The maximum number of threads available to run transient jobs.intgetMaxLeasedThreads()The maximum number of threads that are leased to internal components, as some component may allocate its threads lazily.intgetMaxReservedThreads()intgetMaxThreads()intgetMinThreads()java.lang.StringgetName()protected java.util.concurrent.BlockingQueue<java.lang.Runnable>getQueue()intgetQueueSize()intgetReadyThreads()The fundamental value that represents the number of threads ready to execute transient jobs.intgetReservedThreads()ThreadPoolBudgetgetThreadPoolBudget()intgetThreads()The fundamental value that represents the number of threads currently known by this thread pool.intgetThreadsPriority()doublegetUtilizationRate()The rate between the number ofutilized threadsand the maximum number ofutilizable threads.intgetUtilizedThreads()The number of threads executing transient jobs.booleaninterruptThread(long id)booleanisDaemon()booleanisDetailedDump()booleanisLowOnThreads()Returns whether this thread pool is low on threads.voidjoin()Blocks until the thread pool isstopped.private voidjoinThreads(long stopByNanos)java.lang.ThreadnewThread(java.lang.Runnable runnable)protected voidremoveThread(java.lang.Thread thread)protected voidrunJob(java.lang.Runnable job)Runs the given job in thecurrent thread.voidsetDaemon(boolean daemon)voidsetDetailedDump(boolean detailedDump)voidsetIdleTimeout(int idleTimeout)Set the maximum thread idle time in ms.voidsetLowThreadsThreshold(int lowThreadsThreshold)voidsetMaxThreads(int maxThreads)voidsetMinThreads(int minThreads)voidsetName(java.lang.String name)Sets the name of this thread pool, used as a prefix for the thread names.voidsetQueue(java.util.concurrent.BlockingQueue<java.lang.Runnable> queue)Deprecated.pass the queue to the constructor insteadvoidsetReservedThreads(int reservedThreads)voidsetThreadPoolBudget(ThreadPoolBudget budget)voidsetThreadsPriority(int priority)protected voidstartThread()java.lang.StringtoString()booleantryExecute(java.lang.Runnable task)Attempt to execute a task.-
Methods inherited from class org.eclipse.jetty.util.component.ContainerLifeCycle
addBean, addBean, addEventListener, addManaged, contains, destroy, dump, dump, dump, dump, dumpBeans, dumpObject, dumpObjects, dumpStdErr, dumpThis, getBean, getBeans, getBeans, getContainedBeans, getContainedBeans, isAuto, isManaged, isUnmanaged, manage, removeBean, removeBeans, removeEventListener, setBeans, setStopTimeout, start, stop, unmanage, updateBean, updateBean, updateBeans
-
Methods inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
addLifeCycleListener, getState, getState, getStopTimeout, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, start, stop
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.eclipse.jetty.util.component.Dumpable.DumpableContainer
isDumpable
-
-
-
-
Field Detail
-
LOG
private static final Logger LOG
-
NOOP
private static final java.lang.Runnable NOOP
-
_counts
private final AtomicBiInteger _counts
Encodes thread counts:- Hi
- Total thread count or Integer.MIN_VALUE if the pool is stopping
- Lo
- Net idle threads == idle threads - job queue size. Essentially if positive, this represents the effective number of idle threads, and if negative it represents the demand for more threads
-
_lastShrink
private final java.util.concurrent.atomic.AtomicLong _lastShrink
-
_threads
private final java.util.Set<java.lang.Thread> _threads
-
_joinLock
private final java.lang.Object _joinLock
-
_jobs
private final java.util.concurrent.BlockingQueue<java.lang.Runnable> _jobs
-
_threadGroup
private final java.lang.ThreadGroup _threadGroup
-
_threadFactory
private final java.util.concurrent.ThreadFactory _threadFactory
-
_name
private java.lang.String _name
-
_idleTimeout
private int _idleTimeout
-
_maxThreads
private int _maxThreads
-
_minThreads
private int _minThreads
-
_reservedThreads
private int _reservedThreads
-
_tryExecutor
private TryExecutor _tryExecutor
-
_priority
private int _priority
-
_daemon
private boolean _daemon
-
_detailedDump
private boolean _detailedDump
-
_lowThreadsThreshold
private int _lowThreadsThreshold
-
_budget
private ThreadPoolBudget _budget
-
_runnable
private final java.lang.Runnable _runnable
-
-
Constructor Detail
-
QueuedThreadPool
public QueuedThreadPool()
-
QueuedThreadPool
public QueuedThreadPool(@Name("maxThreads") int maxThreads)
-
QueuedThreadPool
public QueuedThreadPool(@Name("maxThreads") int maxThreads, @Name("minThreads") int minThreads)
-
QueuedThreadPool
public QueuedThreadPool(@Name("maxThreads") int maxThreads, @Name("minThreads") int minThreads, @Name("queue") java.util.concurrent.BlockingQueue<java.lang.Runnable> queue)
-
QueuedThreadPool
public QueuedThreadPool(@Name("maxThreads") int maxThreads, @Name("minThreads") int minThreads, @Name("idleTimeout") int idleTimeout)
-
QueuedThreadPool
public QueuedThreadPool(@Name("maxThreads") int maxThreads, @Name("minThreads") int minThreads, @Name("idleTimeout") int idleTimeout, @Name("queue") java.util.concurrent.BlockingQueue<java.lang.Runnable> queue)
-
QueuedThreadPool
public QueuedThreadPool(@Name("maxThreads") int maxThreads, @Name("minThreads") int minThreads, @Name("idleTimeout") int idleTimeout, @Name("queue") java.util.concurrent.BlockingQueue<java.lang.Runnable> queue, @Name("threadGroup") java.lang.ThreadGroup threadGroup)
-
QueuedThreadPool
public QueuedThreadPool(@Name("maxThreads") int maxThreads, @Name("minThreads") int minThreads, @Name("idleTimeout") int idleTimeout, @Name("reservedThreads") int reservedThreads, @Name("queue") java.util.concurrent.BlockingQueue<java.lang.Runnable> queue, @Name("threadGroup") java.lang.ThreadGroup threadGroup)
-
QueuedThreadPool
public QueuedThreadPool(@Name("maxThreads") int maxThreads, @Name("minThreads") int minThreads, @Name("idleTimeout") int idleTimeout, @Name("reservedThreads") int reservedThreads, @Name("queue") java.util.concurrent.BlockingQueue<java.lang.Runnable> queue, @Name("threadGroup") java.lang.ThreadGroup threadGroup, @Name("threadFactory") java.util.concurrent.ThreadFactory threadFactory)
-
-
Method Detail
-
getThreadPoolBudget
public ThreadPoolBudget getThreadPoolBudget()
- Specified by:
getThreadPoolBudgetin interfaceThreadPool.SizedThreadPool- Returns:
- a ThreadPoolBudget for this sized thread pool, or null of no ThreadPoolBudget can be returned
-
setThreadPoolBudget
public void setThreadPoolBudget(ThreadPoolBudget budget)
-
doStart
protected void doStart() throws java.lang.ExceptionDescription copied from class:ContainerLifeCycleStarts the managed lifecycle beans in the order they were added.- Overrides:
doStartin classContainerLifeCycle- Throws:
java.lang.Exception
-
doStop
protected void doStop() throws java.lang.ExceptionDescription copied from class:ContainerLifeCycleStops the managed lifecycle beans in the reverse order they were added.- Overrides:
doStopin classContainerLifeCycle- Throws:
java.lang.Exception
-
joinThreads
private void joinThreads(long stopByNanos) throws java.lang.InterruptedException- Throws:
java.lang.InterruptedException
-
getIdleTimeout
@ManagedAttribute("maximum time a thread may be idle in ms") public int getIdleTimeout()
- Returns:
- the maximum thread idle time in ms
-
setIdleTimeout
public void setIdleTimeout(int idleTimeout)
Set the maximum thread idle time in ms.
Threads that are idle for longer than this period may be stopped.
- Parameters:
idleTimeout- the maximum thread idle time in ms
-
getMaxThreads
@ManagedAttribute("maximum number of threads in the pool") public int getMaxThreads()
- Specified by:
getMaxThreadsin interfaceThreadPool.SizedThreadPool- Returns:
- the maximum number of threads
-
setMaxThreads
public void setMaxThreads(int maxThreads)
- Specified by:
setMaxThreadsin interfaceThreadPool.SizedThreadPool- Parameters:
maxThreads- the maximum number of threads
-
getMinThreads
@ManagedAttribute("minimum number of threads in the pool") public int getMinThreads()
- Specified by:
getMinThreadsin interfaceThreadPool.SizedThreadPool- Returns:
- the minimum number of threads
-
setMinThreads
public void setMinThreads(int minThreads)
- Specified by:
setMinThreadsin interfaceThreadPool.SizedThreadPool- Parameters:
minThreads- minimum number of threads
-
getReservedThreads
@ManagedAttribute("number of configured reserved threads or -1 for heuristic") public int getReservedThreads()
- Returns:
- number of reserved threads or -1 for heuristically determined
-
setReservedThreads
public void setReservedThreads(int reservedThreads)
- Parameters:
reservedThreads- number of reserved threads or -1 for heuristically determined
-
getName
@ManagedAttribute("name of the thread pool") public java.lang.String getName()
- Returns:
- the name of the this thread pool
-
setName
public void setName(java.lang.String name)
Sets the name of this thread pool, used as a prefix for the thread names.
- Parameters:
name- the name of the this thread pool
-
getThreadsPriority
@ManagedAttribute("priority of threads in the pool") public int getThreadsPriority()
- Returns:
- the priority of the pool threads
-
setThreadsPriority
public void setThreadsPriority(int priority)
- Parameters:
priority- the priority of the pool threads
-
isDaemon
@ManagedAttribute("thread pool uses daemon threads") public boolean isDaemon()
- Returns:
- whether to use daemon threads
- See Also:
Thread.isDaemon()
-
setDaemon
public void setDaemon(boolean daemon)
- Parameters:
daemon- whether to use daemon threads- See Also:
Thread.setDaemon(boolean)
-
isDetailedDump
@ManagedAttribute("reports additional details in the dump") public boolean isDetailedDump()
-
setDetailedDump
public void setDetailedDump(boolean detailedDump)
-
getLowThreadsThreshold
@ManagedAttribute("threshold at which the pool is low on threads") public int getLowThreadsThreshold()
-
setLowThreadsThreshold
public void setLowThreadsThreshold(int lowThreadsThreshold)
-
getQueueSize
@ManagedAttribute("size of the job queue") public int getQueueSize()
- Returns:
- the number of jobs in the queue waiting for a thread
-
getMaxReservedThreads
@ManagedAttribute("maximum number (capacity) of reserved threads") public int getMaxReservedThreads()
- Returns:
- the maximum number (capacity) of reserved threads
- See Also:
ReservedThreadExecutor.getCapacity()
-
getAvailableReservedThreads
@ManagedAttribute("number of available reserved threads") public int getAvailableReservedThreads()
- Returns:
- the number of available reserved threads
- See Also:
ReservedThreadExecutor.getAvailable()
-
getThreads
@ManagedAttribute("number of threads in the pool") public int getThreads()
The fundamental value that represents the number of threads currently known by this thread pool.
This value includes threads that have been leased to internal components, idle threads, reserved threads and threads that are executing transient jobs.
- Specified by:
getThreadsin interfaceThreadPool- Returns:
- the number of threads currently known to the pool
- See Also:
getReadyThreads(),getLeasedThreads()
-
getReadyThreads
@ManagedAttribute("number of threads ready to execute transient jobs") public int getReadyThreads()
The fundamental value that represents the number of threads ready to execute transient jobs.
- Returns:
- the number of threads ready to execute transient jobs
- See Also:
getThreads(),getLeasedThreads(),getUtilizedThreads()
-
getLeasedThreads
@ManagedAttribute("number of threads used by internal components") public int getLeasedThreads()
The fundamental value that represents the number of threads that are leased to internal components, and therefore cannot be used to execute transient jobs.
- Returns:
- the number of threads currently used by internal components
- See Also:
getThreads(),getReadyThreads()
-
getMaxLeasedThreads
@ManagedAttribute("maximum number of threads leased to internal components") public int getMaxLeasedThreads()
The maximum number of threads that are leased to internal components, as some component may allocate its threads lazily.
- Returns:
- the maximum number of threads leased by internal components
- See Also:
getLeasedThreads()
-
getIdleThreads
@ManagedAttribute("number of idle threads but not reserved") public int getIdleThreads()
The number of idle threads, but without including reserved threads.
Prefer
getReadyThreads()for a better representation of "threads ready to execute transient jobs".- Specified by:
getIdleThreadsin interfaceThreadPool- Returns:
- the number of idle threads but not reserved
- See Also:
getReadyThreads()
-
getBusyThreads
@ManagedAttribute("number of threads executing internal and transient jobs") public int getBusyThreads()
The number of threads executing internal and transient jobs.
Prefer
getUtilizedThreads()for a better representation of "threads executing transient jobs".- Returns:
- the number of threads executing internal and transient jobs
- See Also:
getUtilizedThreads()
-
getUtilizedThreads
@ManagedAttribute("number of threads executing transient jobs") public int getUtilizedThreads()
The number of threads executing transient jobs.
- Returns:
- the number of threads executing transient jobs
- See Also:
getReadyThreads()
-
getMaxAvailableThreads
@ManagedAttribute("maximum number of threads available to run transient jobs") public int getMaxAvailableThreads()
The maximum number of threads available to run transient jobs.
- Returns:
- the maximum number of threads available to run transient jobs
-
getUtilizationRate
@ManagedAttribute("utilization rate of threads executing transient jobs") public double getUtilizationRate()
The rate between the number of
utilized threadsand the maximum number ofutilizable threads.A value of
0.0Dmeans that the thread pool is not utilized, while a value of1.0Dmeans that the thread pool is fully utilized to execute transient jobs.- Returns:
- the utilization rate of threads executing transient jobs
-
isLowOnThreads
@ManagedAttribute(value="thread pool is low on threads", readonly=true) public boolean isLowOnThreads()
Returns whether this thread pool is low on threads.
The current formula is:
maxThreads - threads + readyThreads - queueSize <= lowThreadsThreshold
- Specified by:
isLowOnThreadsin interfaceThreadPool- Returns:
- whether the pool is low on threads
- See Also:
getLowThreadsThreshold()
-
execute
public void execute(java.lang.Runnable job)
- Specified by:
executein interfacejava.util.concurrent.Executor- Specified by:
executein interfaceTryExecutor
-
tryExecute
public boolean tryExecute(java.lang.Runnable task)
Description copied from interface:TryExecutorAttempt to execute a task.- Specified by:
tryExecutein interfaceTryExecutor- Parameters:
task- The task to be executed- Returns:
- True IFF the task has been given directly to a thread to execute. The task cannot be queued pending the later availability of a Thread.
-
join
public void join() throws java.lang.InterruptedExceptionBlocks until the thread pool isstopped.- Specified by:
joinin interfaceThreadPool- Throws:
java.lang.InterruptedException- if thread was interrupted
-
ensureThreads
private void ensureThreads()
-
startThread
protected void startThread()
-
addCounts
private boolean addCounts(int deltaThreads, int deltaIdle)
-
newThread
public java.lang.Thread newThread(java.lang.Runnable runnable)
- Specified by:
newThreadin interfacejava.util.concurrent.ThreadFactory
-
removeThread
protected void removeThread(java.lang.Thread thread)
-
dump
public void dump(java.lang.Appendable out, java.lang.String indent) throws java.io.IOExceptionDescription copied from interface:DumpableDump this object (and children) into an Appendable using the provided indent after any new lines. The indent should not be applied to the first object dumped.- Specified by:
dumpin interfaceDumpable- Overrides:
dumpin classContainerLifeCycle- Parameters:
out- The appendable to dump toindent- The indent to apply after any new lines.- Throws:
java.io.IOException- if unable to write to Appendable
-
getCompressedStackTag
private java.lang.String getCompressedStackTag(java.lang.StackTraceElement[] trace)
-
runJob
protected void runJob(java.lang.Runnable job)
Runs the given job in the
current thread.Subclasses may override to perform pre/post actions before/after the job is run.
- Parameters:
job- the job to run
-
getQueue
protected java.util.concurrent.BlockingQueue<java.lang.Runnable> getQueue()
- Returns:
- the job queue
-
setQueue
@Deprecated public void setQueue(java.util.concurrent.BlockingQueue<java.lang.Runnable> queue)
Deprecated.pass the queue to the constructor instead- Parameters:
queue- the job queue
-
interruptThread
@ManagedOperation("interrupts a pool thread") public boolean interruptThread(@Name("id") long id)
- Parameters:
id- the thread ID to interrupt.- Returns:
- true if the thread was found and interrupted.
-
dumpThread
@ManagedOperation("dumps a pool thread stack") public java.lang.String dumpThread(@Name("id") long id)
- Parameters:
id- the thread ID to interrupt.- Returns:
- the stack frames dump
-
toString
public java.lang.String toString()
- Overrides:
toStringin classAbstractLifeCycle
-
-