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
,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 aReservedThreadExecutor
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 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 Modifier and Type Class Description private class
ReservedThreadExecutor.ReservedThread
-
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 Modifier and Type Field Description private int
_capacity
private java.util.concurrent.Executor
_executor
private long
_idleTime
private java.util.concurrent.TimeUnit
_idleTimeUnit
private ThreadPoolBudget.Lease
_lease
private java.util.concurrent.atomic.AtomicInteger
_pending
private java.util.concurrent.atomic.AtomicInteger
_size
private java.util.concurrent.ConcurrentLinkedDeque<ReservedThreadExecutor.ReservedThread>
_stack
private static Logger
LOG
private static java.lang.Runnable
STOP
-
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 void
doStart()
void
doStop()
void
execute(java.lang.Runnable task)
int
getAvailable()
int
getCapacity()
java.util.concurrent.Executor
getExecutor()
long
getIdleTimeoutMs()
int
getPending()
private static int
reservedThreads(java.util.concurrent.Executor executor, int capacity)
void
setIdleTimeout(long idleTime, java.util.concurrent.TimeUnit idleTimeUnit)
Set the idle timeout for shrinking the reserved thread poolprivate void
startReservedThread()
java.lang.String
toString()
boolean
tryExecute(java.lang.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
-
-
-
-
Field Detail
-
LOG
private static final Logger LOG
-
STOP
private static final java.lang.Runnable STOP
-
_executor
private final java.util.concurrent.Executor _executor
-
_capacity
private final int _capacity
-
_stack
private final java.util.concurrent.ConcurrentLinkedDeque<ReservedThreadExecutor.ReservedThread> _stack
-
_size
private final java.util.concurrent.atomic.AtomicInteger _size
-
_pending
private final java.util.concurrent.atomic.AtomicInteger _pending
-
_lease
private ThreadPoolBudget.Lease _lease
-
_idleTime
private long _idleTime
-
_idleTimeUnit
private java.util.concurrent.TimeUnit _idleTimeUnit
-
-
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()
-
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, 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 no timeout.idleTimeUnit
- Time units for idle timeout
-
doStart
public void doStart() throws java.lang.Exception
- Overrides:
doStart
in classAbstractLifeCycle
- Throws:
java.lang.Exception
-
doStop
public void doStop() throws java.lang.Exception
- Overrides:
doStop
in classAbstractLifeCycle
- Throws:
java.lang.Exception
-
execute
public void execute(java.lang.Runnable task) throws java.util.concurrent.RejectedExecutionException
- Specified by:
execute
in interfacejava.util.concurrent.Executor
- Specified by:
execute
in interfaceTryExecutor
- Throws:
java.util.concurrent.RejectedExecutionException
-
tryExecute
public boolean tryExecute(java.lang.Runnable task)
Description copied from interface:TryExecutor
Attempt to execute a task.- Specified by:
tryExecute
in interfaceTryExecutor
- Parameters:
task
- The task to run- Returns:
- True iff a reserved thread was available and has been assigned the task to run.
-
startReservedThread
private void startReservedThread()
-
toString
public java.lang.String toString()
- Overrides:
toString
in classAbstractLifeCycle
-
-