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 aReservedThreadExecutorwill 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
Executorwhen an execution fails. If thesetIdleTimeout(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 classes/interfaces inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
AbstractLifeCycle.AbstractLifeCycleListener
-
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, RUNNING, STARTED, STARTING, STOPPED, STOPPING
-
Fields inherited from interface org.eclipse.jetty.util.thread.TryExecutor
NO_TRY
-
-
Constructor Summary
Constructors Constructor Description ReservedThreadExecutor(Executor executor, int capacity)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voiddoStart()voiddoStop()voidexecute(Runnable task)intgetAvailable()intgetCapacity()ExecutorgetExecutor()longgetIdleTimeoutMs()intgetPending()voidsetIdleTimeout(long idleTime, TimeUnit idleTimeUnit)Set the idle timeout for shrinking the reserved thread poolStringtoString()booleantryExecute(Runnable task)Attempt to execute a task.-
Methods inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
addLifeCycleListener, getState, getState, getStopTimeout, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, setStopTimeout, start, stop
-
-
-
-
Constructor Detail
-
ReservedThreadExecutor
public ReservedThreadExecutor(Executor executor, int capacity)
- 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 Detail
-
getExecutor
public Executor getExecutor()
-
getCapacity
@ManagedAttribute(value="max number of reserved threads", readonly=true) public int getCapacity()
-
getAvailable
@ManagedAttribute(value="available reserved threads", readonly=true) public int getAvailable()
-
getPending
@ManagedAttribute(value="pending reserved threads", readonly=true) public int getPending()
-
getIdleTimeoutMs
@ManagedAttribute(value="idletimeout in MS", readonly=true) public long getIdleTimeoutMs()
-
setIdleTimeout
public void setIdleTimeout(long idleTime, TimeUnit idleTimeUnit)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
public void doStart() throws Exception- Overrides:
doStartin classAbstractLifeCycle- Throws:
Exception
-
doStop
public void doStop() throws Exception- Overrides:
doStopin classAbstractLifeCycle- Throws:
Exception
-
execute
public void execute(Runnable task) throws RejectedExecutionException
- Specified by:
executein interfaceExecutor- Specified by:
executein interfaceTryExecutor- Throws:
RejectedExecutionException
-
tryExecute
public boolean tryExecute(Runnable task)
Description copied from interface:TryExecutorAttempt to execute a task.- Specified by:
tryExecutein interfaceTryExecutor- Parameters:
task- The task to run- Returns:
- True iff a reserved thread was available and has been assigned the task to run.
-
toString
public String toString()
- Overrides:
toStringin classAbstractLifeCycle
-
-