Class QueuedThreadPool
- All Implemented Interfaces:
 Executor, ThreadFactory, Container, Destroyable, Dumpable, Dumpable.DumpableContainer, LifeCycle, ThreadPool, ThreadPool.SizedThreadPool, TryExecutor, VirtualThreads.Configurable
- Direct Known Subclasses:
 MonitoredQueuedThreadPool
The queue of jobs should be unbounded, because critical jobs that have been submitted for execution cannot be rejected due to the queue bound limit. The queue might be temporarily full due to a job submission spike. Furthermore, the same HTTP request may be handled by different jobs, and would be non-optimal to reject a job of a request that is already being handled in favor of a job for a new concurrent request that is not yet handled by the application.
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 unleased jobs such as processing
an HTTP request or a WebSocket frame.
QueuedThreadPool has a ReservedThreadExecutor which leases threads from this pool to be
used by the tryExecute(Runnable) method.  This allows some key optimizations in the
AdaptiveExecutionStrategy.  The pool will avoid
reserving new threads if there are jobs queued.
QueuedThreadPool has getters to take a snapshot of the following fundamental values:
threads: the current number of threads. These threads may execute a job (either internal or unleased), or may be ready to run (either idle or reserved). This number may grow or shrink as the thread pool grows or shrinks.leasedThreads: the number of threads that may be needed to run internal jobs or be reserved for internal jobs. A pool should always be configured with more threads than the number of leased threads. This number is typically constant after this thread pool isstarted.getCurrentReservedThreads(): the number of threads that have been reserved for #tryExecute(Runnable) jobs.getIdleThreads(): the number of threads that are currently waiting for a job.getQueueSize(): the number of jobs, queued waiting for a thread.
Given the definitions above, values derived from combinations of fundamental values include:
getBusyThreads(): the number threads executing tasks.getUtilizedThreads(): the number threads executing unleased tasks.
- 
Nested Class Summary
Nested classes/interfaces inherited from class AbstractLifeCycle
AbstractLifeCycle.AbstractLifeCycleListener, AbstractLifeCycle.StopExceptionNested classes/interfaces inherited from interface Container
Container.InheritedListener, Container.ListenerNested classes/interfaces inherited from interface Dumpable
Dumpable.DumpableContainer, Dumpable.DumpAppendableNested classes/interfaces inherited from interface LifeCycle
LifeCycle.ListenerNested classes/interfaces inherited from interface ThreadPool
ThreadPool.SizedThreadPoolNested classes/interfaces inherited from interface TryExecutor
TryExecutor.NoTryExecutor - 
Field Summary
Fields inherited from interface TryExecutor
NO_TRY - 
Constructor Summary
ConstructorsConstructorDescriptionQueuedThreadPool(int maxThreads) QueuedThreadPool(int maxThreads, int minThreads) QueuedThreadPool(int maxThreads, int minThreads, int idleTimeout) QueuedThreadPool(int maxThreads, int minThreads, int idleTimeout, int reservedThreads, BlockingQueue<Runnable> queue, ThreadGroup threadGroup) QueuedThreadPool(int maxThreads, int minThreads, int idleTimeout, int reservedThreads, BlockingQueue<Runnable> queue, ThreadGroup threadGroup, ThreadFactory threadFactory) QueuedThreadPool(int maxThreads, int minThreads, int idleTimeout, BlockingQueue<Runnable> queue) QueuedThreadPool(int maxThreads, int minThreads, int idleTimeout, BlockingQueue<Runnable> queue, ThreadGroup threadGroup) QueuedThreadPool(int maxThreads, int minThreads, BlockingQueue<Runnable> queue)  - 
Method Summary
Modifier and TypeMethodDescriptionprotected 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(Appendable out, String indent) Dump this object (and children) into an Appendable using the provided indent after any new lines.dumpThread(long id) protected booleanevict()Determines whether to evict the current thread from the pool.voidintDeprecated, for removal: This API element is subject to removal in a future version.intThe number of threads executing leased and unleased jobs.intintThe number of idle threads, waiting for tasks to be submitted toexecute(Runnable).intintThe number of threads that are leased, and therefore cannot be used to execute unleased jobs.intintDeprecated, for removal: This API element is subject to removal in a future version.The term available can be applied to either idle or reserved threads, but not both.intintDeprecated, for removal: This API element is subject to removal in a future version.intintintgetName()protected BlockingQueue<Runnable> getQueue()intintDeprecated, for removal: This API element is subject to removal in a future version.The combination of idle and reserved threads is essentially meaningless.intlongintThe number of threads currently started by this thread pool.intdoubleThe rate between the number ofutilized threadsand the maximum number ofutilizable threads.intThe number of threads executing unleased tasks.booleaninterruptThread(long id) booleanisDaemon()booleanbooleanReturns whether this thread pool is low on threads.voidjoin()Blocks until the thread pool isLifeCyclestopped.protected voidprotected voidremoveThread(Thread thread) protected voidRuns 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) voidsetMaxEvictCount(int evictCount) Returns the maximum number of idle threads that are evicted for every idle timeout period, thus shrinking this thread pool towards itsminimum number of threads.voidsetMaxThreads(int maxThreads) voidsetMinThreads(int minThreads) voidSets the name of this thread pool, used as a prefix for the thread names.voidsetReservedThreads(int reservedThreads) Set number of reserved threads or -1 for heuristically determined.voidsetStopTimeout(long stopTimeout) voidsetThreadPoolBudget(ThreadPoolBudget budget) voidsetThreadsPriority(int priority) Set the priority of the pool threads.voidsetVirtualThreadsExecutor(Executor executor) protected voidtoString()booleantryExecute(Runnable task) Attempt to execute a task.Methods inherited from class ContainerLifeCycle
addBean, addBean, addEventListener, addManaged, contains, destroy, dump, dump, dumpObjects, dumpStdErr, getBean, getBeans, getBeans, getContainedBeans, getContainedBeans, installBean, installBean, isAuto, isManaged, isUnmanaged, manage, removeBean, removeBeans, removeEventListener, setBeans, start, stop, unmanage, updateBean, updateBean, updateBeans, updateBeansMethods inherited from class AbstractLifeCycle
getEventListeners, getState, getState, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, setEventListeners, start, stopMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Container
getCachedBeans, getEventListenersMethods inherited from interface Dumpable.DumpableContainer
isDumpableMethods inherited from interface VirtualThreads.Configurable
isUseVirtualThreads, setUseVirtualThreads 
- 
Constructor Details
- 
QueuedThreadPool
public QueuedThreadPool() - 
QueuedThreadPool
 - 
QueuedThreadPool
 - 
QueuedThreadPool
public QueuedThreadPool(@Name("maxThreads") int maxThreads, @Name("minThreads") int minThreads, @Name("queue") BlockingQueue<Runnable> queue)  - 
QueuedThreadPool
 - 
QueuedThreadPool
 - 
QueuedThreadPool
public QueuedThreadPool(@Name("maxThreads") int maxThreads, @Name("minThreads") int minThreads, @Name("idleTimeout") int idleTimeout, @Name("queue") BlockingQueue<Runnable> queue, @Name("threadGroup") ThreadGroup threadGroup)  - 
QueuedThreadPool
public QueuedThreadPool(@Name("maxThreads") int maxThreads, @Name("minThreads") int minThreads, @Name("idleTimeout") int idleTimeout, @Name("reservedThreads") int reservedThreads, @Name("queue") BlockingQueue<Runnable> queue, @Name("threadGroup") ThreadGroup threadGroup)  - 
QueuedThreadPool
public QueuedThreadPool(@Name("maxThreads") int maxThreads, @Name("minThreads") int minThreads, @Name("idleTimeout") int idleTimeout, @Name("reservedThreads") int reservedThreads, @Name("queue") BlockingQueue<Runnable> queue, @Name("threadGroup") ThreadGroup threadGroup, @Name("threadFactory") ThreadFactory threadFactory)  
 - 
 - 
Method Details
- 
getThreadPoolBudget
- Specified by:
 getThreadPoolBudgetin interfaceThreadPool.SizedThreadPool- Returns:
 - a ThreadPoolBudget for this sized thread pool, or null of no ThreadPoolBudget can be returned
 
 - 
setThreadPoolBudget
 - 
setStopTimeout
public void setStopTimeout(long stopTimeout)  - 
getStopTimeout
public long getStopTimeout() - 
doStart
Description copied from class:ContainerLifeCycleStarts the managed lifecycle beans in the order they were added.- Overrides:
 doStartin classContainerLifeCycle- Throws:
 Exception- If there was a problem starting. Will cause a transition to FAILED state
 - 
doStop
Description copied from class:ContainerLifeCycleStops the managed lifecycle beans in the reverse order they were added.- Overrides:
 doStopin classContainerLifeCycle- Throws:
 Exception- If there was a problem stopping. Will cause a transition to FAILED state
 - 
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
- 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
- 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) Set number of reserved threads or -1 for heuristically determined.- Parameters:
 reservedThreads- number of reserved threads or -1 for heuristically determined
 - 
getName
- Returns:
 - the name of this thread pool
 
 - 
setName
Sets the name of this thread pool, used as a prefix for the thread names.
- Parameters:
 name- the name of this thread pool
 - 
getThreadsPriority
- Returns:
 - the priority of the pool threads
 
 - 
setThreadsPriority
public void setThreadsPriority(int priority) Set the priority of the pool threads.- Parameters:
 priority- the priority of the pool threads
 - 
isDaemon
- Returns:
 - whether to use daemon threads
 - See Also:
 
 - 
setDaemon
public void setDaemon(boolean daemon) - Parameters:
 daemon- whether to use daemon threads- See Also:
 
 - 
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)  - 
getVirtualThreadsExecutor
- Specified by:
 getVirtualThreadsExecutorin interfaceVirtualThreads.Configurable- Returns:
 - the 
Executorto use to execute tasks in virtual threads 
 - 
setVirtualThreadsExecutor
- Specified by:
 setVirtualThreadsExecutorin interfaceVirtualThreads.Configurable- Parameters:
 executor- theExecutorto use to execute tasks in virtual threads- See Also:
 
 - 
setMaxEvictCount
public void setMaxEvictCount(int evictCount) Returns the maximum number of idle threads that are evicted for every idle timeout period, thus shrinking this thread pool towards its
minimum number of threads. The default value is1.For example, consider a thread pool with
minThread=2,maxThread=20,idleTimeout=5000andmaxEvictCount=3. Let's assume all 20 threads are executing a task, and they all finish their own tasks at the same time and no more tasks are submitted; then, 3 threads will be evicted, while the other 17 will wait another idle timeout; then another 3 threads will be evicted, and so on untilminThreads=2will be reached.- Parameters:
 evictCount- the maximum number of idle threads to evict in one idle timeout period
 - 
getMaxEvictCount
@ManagedAttribute("maximum number of idle threads to evict in one idle timeout period") public int getMaxEvictCount()- Returns:
 - the maximum number of idle threads to evict in one idle timeout period
 
 - 
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 that have actually been reserved.
The value will always be less than or equal to 
getReservedThreads(). - See Also:
 
 - 
getCurrentReservedThreads
- Returns:
 - the number of available reserved threads that are currently reserved
 - See Also:
 
 - 
getAvailableReservedThreads
@Deprecated(forRemoval=true, since="12.1.0") @ManagedAttribute("number of available reserved threads") public int getAvailableReservedThreads()Deprecated, for removal: This API element is subject to removal in a future version.usegetCurrentReservedThreads()instead- Returns:
 - the number of available reserved threads that are currently reserved
 - See Also:
 
 - 
getThreads
The number of threads currently started by this thread pool.
This value includes threads that have been leased threads, idle threads, reserved threads and threads that are executing unleased jobs.
- Specified by:
 getThreadsin interfaceThreadPool- Returns:
 - the number of threads currently known to the pool
 - See Also:
 
 - 
getReadyThreads
@Deprecated(forRemoval=true, since="12.1.0") @ManagedAttribute("number of threads ready to execute unleased jobs") public int getReadyThreads()Deprecated, for removal: This API element is subject to removal in a future version.The combination of idle and reserved threads is essentially meaningless.The number of threads ready to execute unleased jobs.
- Returns:
 - the number of threads ready to execute unleased jobs
 - See Also:
 
 - 
getLeasedThreads
@ManagedAttribute("number of threads leased for use by jetty components") public int getLeasedThreads()The number of threads that are leased, and therefore cannot be used to execute unleased jobs.
- Returns:
 - the number of threads currently used by internal components
 - See Also:
 
 - 
getMaxLeasedThreads
@Deprecated(forRemoval=true, since="12.1.0") @ManagedAttribute("maximum number of threads leased to internal components") public int getMaxLeasedThreads()Deprecated, for removal: This API element is subject to removal in a future version.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:
 
 - 
getIdleThreads
The number of idle threads, waiting for tasks to be submitted to
execute(Runnable).Note that
getCurrentReservedThreads()are not counted as idle threads.- Specified by:
 getIdleThreadsin interfaceThreadPool- Returns:
 - the number of idle threads
 
 - 
getBusyThreads
@ManagedAttribute("number of threads executing internal and unleased jobs") public int getBusyThreads()The number of threads executing leased and unleased jobs.
Prefer
getUtilizedThreads()for a representation of "threads executing unleased jobs".- Returns:
 - the number of threads executing internal and unleased jobs
 - See Also:
 
 - 
getUtilizedThreads
The number of threads executing unleased tasks.
- Returns:
 - the number of threads executing unleased tasks
 - See Also:
 
 - 
getMaxAvailableThreads
@ManagedAttribute("maximum number of threads available to run unleased jobs (deprecated") @Deprecated(forRemoval=true, since="12.1.0") public int getMaxAvailableThreads()Deprecated, for removal: This API element is subject to removal in a future version.The term available can be applied to either idle or reserved threads, but not both. UsegetCurrentReservedThreads()orgetIdleThreads()instead.The maximum number of threads available to run unleased jobs.
- Returns:
 - the maximum number of threads available to run unleased jobs
 
 - 
getUtilizationRate
@ManagedAttribute("utilization rate of threads executing unleased 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 unleased jobs.- Returns:
 - the utilization rate of threads executing unleased 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 + idleThreads - queueSize <= lowThreadsThreshold
- Specified by:
 isLowOnThreadsin interfaceThreadPool- Returns:
 - whether the pool is low on threads
 - See Also:
 
 - 
execute
- Specified by:
 executein interfaceExecutor- Specified by:
 executein interfaceTryExecutor
 - 
tryExecute
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
Blocks until the thread pool isLifeCyclestopped.- Specified by:
 joinin interfaceThreadPool- Throws:
 InterruptedException- if thread was interrupted
 - 
startThread
protected void startThread() - 
newThread
- Specified by:
 newThreadin interfaceThreadFactory
 - 
removeThread
 - 
dump
Description 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:
 IOException- if unable to write to Appendable
 - 
runJob
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
 - 
onJobFailure
 - 
evict
protected boolean evict()Determines whether to evict the current thread from the pool.
- Returns:
 - whether the current thread should be evicted
 
 - 
getQueue
- Returns:
 - the job queue
 
 - 
interruptThread
- Parameters:
 id- the thread ID to interrupt.- Returns:
 - true if the thread was found and interrupted.
 
 - 
dumpThread
- Parameters:
 id- the thread ID to interrupt.- Returns:
 - the stack frames dump
 
 - 
toString
- Overrides:
 toStringin classAbstractLifeCycle
 
 - 
 
getCurrentReservedThreads()instead