java.util.concurrent.Executor, LifeCycle@ManagedObject("A pool for reserved threads") public class ReservedThreadExecutor extends AbstractLifeCycle implements java.util.concurrent.Executor
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.AbstractLifeCycleListenerLifeCycle.ListenerFAILED, RUNNING, STARTED, STARTING, STOP_ON_FAILURE, STOPPED, STOPPING| Constructor | Description |
|---|---|
ReservedThreadExecutor(java.util.concurrent.Executor executor) |
|
ReservedThreadExecutor(java.util.concurrent.Executor executor,
int capacity) |
|
ReservedThreadExecutor(java.util.concurrent.Executor executor,
int capacity,
java.lang.Object owner) |
| 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() |
|
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
|
java.lang.String |
toString() |
|
boolean |
tryExecute(java.lang.Runnable task) |
addLifeCycleListener, getState, getState, getStopTimeout, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, setStopTimeout, start, stoppublic ReservedThreadExecutor(java.util.concurrent.Executor executor)
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 ReservedThreadExecutor(java.util.concurrent.Executor executor,
int capacity,
java.lang.Object owner)
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 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 AbstractLifeCyclejava.lang.Exceptionpublic void doStop()
throws java.lang.Exception
doStop in class AbstractLifeCyclejava.lang.Exceptionpublic void execute(java.lang.Runnable task)
throws java.util.concurrent.RejectedExecutionException
execute in interface java.util.concurrent.Executorjava.util.concurrent.RejectedExecutionExceptionpublic boolean tryExecute(java.lang.Runnable task)
task - The task to runpublic java.lang.String toString()
toString in class java.lang.ObjectCopyright © 1995–2017 Webtide. All rights reserved.