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:
java.util.concurrent.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
Executorpassed to the constructor. Whenever 1 or more reserved threads have been idle for more thangetIdleTimeoutMs()then one reserved thread will return to the external Executor.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classReservedThreadExecutor.ReservedThreadprivate static classReservedThreadExecutor.State-
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.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 Modifier and Type Field Description private int_capacityprivate AtomicBiInteger_countprivate java.util.concurrent.Executor_executorprivate long_idleTimeNanosprivate java.util.concurrent.atomic.AtomicLong_lastEmptyTimeprivate ThreadPoolBudget.Lease_leaseprivate java.util.concurrent.SynchronousQueue<java.lang.Runnable>_queueprivate java.util.Set<ReservedThreadExecutor.ReservedThread>_threadsprivate static longDEFAULT_IDLE_TIMEOUTprivate static LoggerLOGprivate static java.lang.RunnableSTOP-
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(java.util.concurrent.Executor executor, int capacity)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voiddoStart()voiddoStop()voiddump(java.lang.Appendable out, java.lang.String indent)Dump this object (and children) into an Appendable using the provided indent after any new lines.voidexecute(java.lang.Runnable task)intgetAvailable()intgetCapacity()java.util.concurrent.ExecutorgetExecutor()longgetIdleTimeoutMs()intgetPending()private static intreservedThreads(java.util.concurrent.Executor executor, int capacity)voidsetIdleTimeout(long idleTime, java.util.concurrent.TimeUnit idleTimeUnit)Set the idle timeout for shrinking the reserved thread poolprivate voidstartReservedThread()java.lang.StringtoString()booleantryExecute(java.lang.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 Detail
-
LOG
private static final Logger LOG
-
DEFAULT_IDLE_TIMEOUT
private static final long DEFAULT_IDLE_TIMEOUT
-
STOP
private static final java.lang.Runnable STOP
-
_executor
private final java.util.concurrent.Executor _executor
-
_capacity
private final int _capacity
-
_threads
private final java.util.Set<ReservedThreadExecutor.ReservedThread> _threads
-
_queue
private final java.util.concurrent.SynchronousQueue<java.lang.Runnable> _queue
-
_count
private final AtomicBiInteger _count
-
_lastEmptyTime
private final java.util.concurrent.atomic.AtomicLong _lastEmptyTime
-
_lease
private ThreadPoolBudget.Lease _lease
-
_idleTimeNanos
private long _idleTimeNanos
-
-
Constructor Detail
-
ReservedThreadExecutor
public ReservedThreadExecutor(java.util.concurrent.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
-
reservedThreads
private static int reservedThreads(java.util.concurrent.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.- Returns:
- the number of reserved threads that would be used by a ReservedThreadExecutor constructed with these arguments.
-
getExecutor
public java.util.concurrent.Executor getExecutor()
-
getCapacity
@ManagedAttribute(value="max number of reserved threads", readonly=true) public int getCapacity()
- Returns:
- the maximum number of reserved threads
-
getAvailable
@ManagedAttribute(value="available reserved threads", readonly=true) public int getAvailable()
- Returns:
- the number of threads available to
tryExecute(Runnable)
-
getPending
@ManagedAttribute(value="pending reserved threads", readonly=true) public int getPending()
-
getIdleTimeoutMs
@ManagedAttribute(value="idle timeout in ms", readonly=true) public long getIdleTimeoutMs()
-
setIdleTimeout
public void setIdleTimeout(long idleTime, java.util.concurrent.TimeUnit idleTimeUnit)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
public void doStart() throws java.lang.Exception- Overrides:
doStartin classAbstractLifeCycle- Throws:
java.lang.Exception
-
doStop
public void doStop() throws java.lang.Exception- Overrides:
doStopin classAbstractLifeCycle- Throws:
java.lang.Exception
-
execute
public void execute(java.lang.Runnable task) throws java.util.concurrent.RejectedExecutionException- Specified by:
executein interfacejava.util.concurrent.Executor- Specified by:
executein interfaceTryExecutor- Throws:
java.util.concurrent.RejectedExecutionException
-
tryExecute
public boolean tryExecute(java.lang.Runnable task)
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
public void dump(java.lang.Appendable out, java.lang.String indent) throws java.io.IOExceptionDescription 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.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classAbstractLifeCycle
-
-