@ManagedObject(value="A pool for reserved threads") public class ReservedThreadExecutor extends AbstractLifeCycle implements TryExecutor
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.
Modifier and Type | Class and Description |
---|---|
private class |
ReservedThreadExecutor.ReservedThread |
AbstractLifeCycle.AbstractLifeCycleListener
TryExecutor.NoTryExecutor
LifeCycle.Listener
Modifier and Type | Field and 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 |
FAILED, RUNNING, STARTED, STARTING, STOP_ON_FAILURE, STOPPED, STOPPING
NO_TRY
Constructor and Description |
---|
ReservedThreadExecutor(java.util.concurrent.Executor executor,
int capacity) |
Modifier and Type | Method and 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 pool
|
private void |
startReservedThread() |
java.lang.String |
toString() |
boolean |
tryExecute(java.lang.Runnable task)
Attempt to execute a task.
|
addLifeCycleListener, getState, getState, getStopTimeout, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, setStopTimeout, start, stop
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
asTryExecutor
private static final Logger LOG
private static final java.lang.Runnable STOP
private final java.util.concurrent.Executor _executor
private final int _capacity
private final java.util.concurrent.ConcurrentLinkedDeque<ReservedThreadExecutor.ReservedThread> _stack
private final java.util.concurrent.atomic.AtomicInteger _size
private final java.util.concurrent.atomic.AtomicInteger _pending
private ThreadPoolBudget.Lease _lease
private long _idleTime
private java.util.concurrent.TimeUnit _idleTimeUnit
public ReservedThreadExecutor(java.util.concurrent.Executor executor, int capacity)
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.private static int reservedThreads(java.util.concurrent.Executor executor, int capacity)
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.public java.util.concurrent.Executor getExecutor()
@ManagedAttribute(value="max number of reserved threads", readonly=true) public int getCapacity()
@ManagedAttribute(value="available reserved threads", readonly=true) public int getAvailable()
@ManagedAttribute(value="pending reserved threads", readonly=true) public int getPending()
@ManagedAttribute(value="idletimeout in MS", readonly=true) public long getIdleTimeoutMs()
public void setIdleTimeout(long idleTime, java.util.concurrent.TimeUnit idleTimeUnit)
idleTime
- Time to wait before shrinking, or 0 for no timeout.idleTimeUnit
- Time units for idle timeoutpublic void doStart() throws java.lang.Exception
doStart
in class AbstractLifeCycle
java.lang.Exception
public void doStop() throws java.lang.Exception
doStop
in class AbstractLifeCycle
java.lang.Exception
public void execute(java.lang.Runnable task) throws java.util.concurrent.RejectedExecutionException
execute
in interface java.util.concurrent.Executor
execute
in interface TryExecutor
java.util.concurrent.RejectedExecutionException
public boolean tryExecute(java.lang.Runnable task)
TryExecutor
tryExecute
in interface TryExecutor
task
- The task to runprivate void startReservedThread()
public java.lang.String toString()
toString
in class java.lang.Object