@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.
AbstractLifeCycle.AbstractLifeCycleListener
TryExecutor.NoTryExecutor
LifeCycle.Listener
FAILED, RUNNING, STARTED, STARTING, 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() |
void |
setIdleTimeout(long idleTime,
java.util.concurrent.TimeUnit idleTimeUnit)
Set the idle timeout for shrinking the reserved thread pool
|
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
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.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 runpublic java.lang.String toString()
toString
in class AbstractLifeCycle
Copyright © 1995–2023 Webtide. All rights reserved.