Package org.eclipse.jetty.util.thread
Class ReservedThreadExecutor
java.lang.Object
org.eclipse.jetty.util.component.AbstractLifeCycle
org.eclipse.jetty.util.thread.ReservedThreadExecutor
- All Implemented Interfaces:
Executor,LifeCycle,TryExecutor
@ManagedObject("A pool for reserved threads")
public class ReservedThreadExecutor
extends AbstractLifeCycle
implements TryExecutor
An Executor using preallocated/reserved Threads from a wrapped Executor.
Calls to execute(Runnable) on a ReservedThreadExecutor will either succeed
with a Thread immediately being assigned the Runnable task, or fail if no Thread is
available.
Threads are reserved lazily, with a new reserved thread being allocated from a
wrapped Executor when an execution fails. If the setIdleTimeout(long, TimeUnit)
is set to non zero (default 1 minute), then the reserved thread pool will shrink by 1 thread
whenever it has been idle for that period.
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
AbstractLifeCycle.AbstractLifeCycleListenerNested classes/interfaces inherited from interface org.eclipse.jetty.util.component.LifeCycle
LifeCycle.ListenerNested classes/interfaces inherited from interface org.eclipse.jetty.util.thread.TryExecutor
TryExecutor.NoTryExecutor -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final intprivate final Executorprivate longprivate TimeUnitprivate ThreadPoolBudget.Leaseprivate final AtomicIntegerprivate final AtomicIntegerprivate final ConcurrentLinkedDeque<ReservedThreadExecutor.ReservedThread> private static final Loggerprivate static final RunnableFields inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
FAILED, RUNNING, STARTED, STARTING, STOPPED, STOPPINGFields inherited from interface org.eclipse.jetty.util.thread.TryExecutor
NO_TRY -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoiddoStart()voiddoStop()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 org.eclipse.jetty.util.component.AbstractLifeCycle
addLifeCycleListener, getState, getState, getStopTimeout, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, setStopTimeout, start, stop
-
Field Details
-
LOG
-
STOP
-
_executor
-
_capacity
private final int _capacity -
_stack
-
_size
-
_pending
-
_lease
-
_idleTime
private long _idleTime -
_idleTimeUnit
-
-
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 no 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() -
toString
- Overrides:
toStringin classAbstractLifeCycle
-