Class ReservedThreadExecutor
java.lang.Object
org.eclipse.jetty.util.component.AbstractLifeCycle
org.eclipse.jetty.util.thread.ReservedThreadExecutor
- All Implemented Interfaces:
Executor, Dumpable, LifeCycle, TryExecutor
@ManagedObject("A pool for reserved threads")
public class ReservedThreadExecutor
extends AbstractLifeCycle
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 ClassesModifier and TypeClassDescriptionprivate classprivate static enumNested classes/interfaces inherited from class AbstractLifeCycle
AbstractLifeCycle.AbstractLifeCycleListenerNested classes/interfaces inherited from interface Dumpable
Dumpable.DumpableContainerNested classes/interfaces inherited from interface LifeCycle
LifeCycle.ListenerNested classes/interfaces inherited from interface TryExecutor
TryExecutor.NoTryExecutor -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final intprivate final AtomicBiIntegerprivate final Executorprivate longprivate final AtomicLongprivate ThreadPoolBudget.Leaseprivate final SynchronousQueue<Runnable> private final Set<ReservedThreadExecutor.ReservedThread> private static final longprivate static final Loggerprivate static final RunnableFields inherited from interface TryExecutor
NO_TRY -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoiddoStart()voiddoStop()voiddump(Appendable out, String indent) Dump this object (and children) into an Appendable using the provided indent after any new lines.voidintintlongintprivate static intreservedThreads(Executor executor, int capacity) voidsetIdleTimeout(long idleTime, TimeUnit idleTimeUnit) Set the idle timeout for shrinking the reserved thread poolprivate voidtoString()booleantryExecute(Runnable task) Executes the given task if and only if a reserved thread is available.Methods inherited from class AbstractLifeCycle
addLifeCycleListener, getState, getState, getStopTimeout, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, setStopTimeout, start, stop
-
Field Details
-
LOG
-
DEFAULT_IDLE_TIMEOUT
private static final long DEFAULT_IDLE_TIMEOUT -
STOP
-
_executor
-
_capacity
private final int _capacity -
_threads
-
_queue
-
_count
-
_lastEmptyTime
-
_lease
-
_idleTimeNanos
private long _idleTimeNanos
-
-
Constructor Details
-
ReservedThreadExecutor
- 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.
-
-
Method Details
-
reservedThreads
- 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
-
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
- Overrides:
doStartin classAbstractLifeCycle- Throws:
Exception
-
doStop
- Overrides:
doStopin classAbstractLifeCycle- Throws:
Exception
-
execute
- Specified by:
executein interfaceExecutor- Specified by:
executein interfaceTryExecutor- Throws:
RejectedExecutionException
-
tryExecute
Executes the given task if and only if a reserved thread is available.
- Specified by:
tryExecutein 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.
-
startReservedThread
private void startReservedThread() -
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- 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:
toStringin classAbstractLifeCycle
-