Package org.eclipse.jetty.util.thread
Class ReservedThreadExecutor
java.lang.Object
org.eclipse.jetty.util.component.AbstractLifeCycle
org.eclipse.jetty.util.component.ContainerLifeCycle
org.eclipse.jetty.util.thread.ReservedThreadExecutor
- All Implemented Interfaces:
Executor
,Container
,Destroyable
,Dumpable
,Dumpable.DumpableContainer
,LifeCycle
,TryExecutor
@ManagedObject("A pool for reserved threads")
public class ReservedThreadExecutor
extends ContainerLifeCycle
implements TryExecutor, Dumpable
A TryExecutor using pre-allocated/reserved threads from an external Executor.
Calls to tryExecute(Runnable)
on ReservedThreadExecutor will either
succeed with a reserved thread immediately being assigned the task, or fail if
no reserved thread is available.
Threads are reserved lazily, with new reserved threads being allocated from the external
Executor
passed to the constructor. Whenever 1 or more reserved threads have been
idle for more than getIdleTimeoutMs()
then one reserved thread will return to
the external Executor.
-
Nested Class Summary
Nested classes/interfaces inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
AbstractLifeCycle.AbstractLifeCycleListener, AbstractLifeCycle.StopException
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.TryExecutor
TryExecutor.NoTryExecutor
-
Field Summary
Fields inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
FAILED, STARTED, STARTING, STOPPED, STOPPING
Fields inherited from interface org.eclipse.jetty.util.thread.TryExecutor
NO_TRY
-
Constructor Summary
ConstructorDescriptionReservedThreadExecutor
(Executor executor, int capacity) ReservedThreadExecutor
(Executor executor, int capacity, int minSize) ReservedThreadExecutor
(Executor executor, int capacity, int minSize, int maxPending) -
Method Summary
Modifier and TypeMethodDescriptionvoid
doStart()
Starts the managed lifecycle beans in the order they were added.void
doStop()
Stops the managed lifecycle beans in the reverse order they were added.void
dump
(Appendable out, String indent) Dump this object (and children) into an Appendable using the provided indent after any new lines.void
int
int
long
int
Deprecated.static int
reservedThreads
(Executor executor, int capacity) Get the heuristic number of reserved threads.void
setIdleTimeout
(long idleTime, TimeUnit idleTimeUnit) Set the idle timeout for shrinking the reserved thread pooltoString()
boolean
tryExecute
(Runnable task) Executes the given task if and only if a reserved thread is available.Methods inherited from class org.eclipse.jetty.util.component.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, updateBeans
Methods inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
getEventListeners, getState, getState, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, setEventListeners, 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.Container
getCachedBeans, getEventListeners
Methods inherited from interface org.eclipse.jetty.util.component.Dumpable.DumpableContainer
isDumpable
-
Constructor Details
-
ReservedThreadExecutor
- Parameters:
executor
- The executor to use to obtain threadscapacity
- The number of threads that can be reserved. If less than 0 then capacity is calculated based on a heuristic from the number of available processors and thread pool type.
-
ReservedThreadExecutor
- Parameters:
executor
- The executor to use to obtain threadscapacity
- The number of threads that can be reserved. If less than 0 then capacity is calculated based on a heuristic from the number of available processors and thread pool type.minSize
- The minimum number of reserve Threads that the algorithm tries to maintain, or -1 for a heuristic value.
-
ReservedThreadExecutor
- Parameters:
executor
- The executor to use to obtain threadscapacity
- The number of threads that can be reserved. If less than 0 then capacity is calculated based on a heuristic from the number of available processors and thread pool type.minSize
- The minimum number of reserve Threads that the algorithm tries to maintain, or -1 for a heuristic value.maxPending
- The maximum number of reserved Threads to start, or -1 for no limit.
-
-
Method Details
-
reservedThreads
Get the heuristic number of reserved threads.- Parameters:
executor
- The executor to use to obtain threadscapacity
- The number of threads to preallocate, If less than 0 then capacity is calculated based on a heuristic from the number of available processors and thread pool size.- Returns:
- the number of reserved threads that would be used by a ReservedThreadExecutor constructed with these arguments.
-
getExecutor
-
getCapacity
- Returns:
- the maximum number of reserved threads
-
getAvailable
- Returns:
- the number of threads available to
tryExecute(Runnable)
-
getPending
@ManagedAttribute(value="pending reserved threads (deprecated)", readonly=true) @Deprecated public int getPending()Deprecated. -
getIdleTimeoutMs
-
setIdleTimeout
Set the idle timeout for shrinking the reserved thread pool- Parameters:
idleTime
- Time to wait before shrinking, or 0 for default timeout.idleTimeUnit
- Time units for idle timeout
-
doStart
Description copied from class:ContainerLifeCycle
Starts the managed lifecycle beans in the order they were added.- Overrides:
doStart
in classContainerLifeCycle
- Throws:
AbstractLifeCycle.StopException
- If thrown, the lifecycle will immediately be stopped.Exception
- If there was a problem starting. Will cause a transition to FAILED state
-
doStop
Description copied from class:ContainerLifeCycle
Stops the managed lifecycle beans in the reverse order they were added.- Overrides:
doStop
in classContainerLifeCycle
- Throws:
Exception
- If there was a problem stopping. Will cause a transition to FAILED state
-
execute
- Specified by:
execute
in interfaceExecutor
- Specified by:
execute
in interfaceTryExecutor
- Throws:
RejectedExecutionException
-
tryExecute
Executes the given task if and only if a reserved thread is available.
- Specified by:
tryExecute
in interfaceTryExecutor
- Parameters:
task
- the task to run- Returns:
- true if and only if a reserved thread was available and has been assigned the task to run.
-
dump
Description copied from interface:Dumpable
Dump 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:
dump
in interfaceDumpable
- Overrides:
dump
in classContainerLifeCycle
- Parameters:
out
- The appendable to dump toindent
- The indent to apply after any new lines.- Throws:
IOException
- if unable to write to Appendable
-
toString
- Overrides:
toString
in classAbstractLifeCycle
-