Top Description Inners Fields Constructors Methods
org.eclipse.jetty.util.thread

public Class ReservedThreadExecutor

extends AbstractLifeCycle
implements TryExecutor, Dumpable
Class Inheritance
All Implemented Interfaces
org.eclipse.jetty.util.component.Dumpable, org.eclipse.jetty.util.thread.TryExecutor, java.util.concurrent.Executor
Annotations
@ManagedObject:A pool for reserved threads
Imports
java.io.IOException, java.util.Objects, .Set, java.util.concurrent.ConcurrentHashMap, .Executor, .RejectedExecutionException, .SynchronousQueue, .TimeUnit, java.util.concurrent.atomic.AtomicLong, java.util.stream.Collectors, org.eclipse.jetty.util.AtomicBiInteger, .NanoTime, .ProcessorUtils, .VirtualThreads, org.eclipse.jetty.util.annotation.ManagedAttribute, .ManagedObject, org.eclipse.jetty.util.component.AbstractLifeCycle, .Dumpable, .DumpableCollection, org.slf4j.Logger, .LoggerFactory

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 Executor passed to the constructor. Whenever 1 or more reserved threads have been idle for more than getIdleTimeoutMs() then one reserved thread will return to the external Executor.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
private static enum

Field Summary

Modifier and TypeField and Description
private final int
private final AtomicBiInteger
private final Executor
private long
private final AtomicLong
private ThreadPoolBudget.Lease
private final SynchronousQueue<Runnable>
private final Set<ReservedThreadExecutor.ReservedThread>
private static final long
private static final Logger
private static final Runnable
Inherited from org.eclipse.jetty.util.component.AbstractLifeCycle:
FAILEDSTARTEDSTARTINGSTOPPEDSTOPPING

Constructor Summary

AccessConstructor and Description
public
ReservedThreadExecutor(Executor
The executor to use to obtain threads
executor
,
int
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.
capacity
)

Method Summary

Modifier and TypeMethod and Description
public void
doStart()

Overrides org.eclipse.jetty.util.component.AbstractLifeCycle.doStart.

Method to override to start the lifecycle
public void
doStop()

Overrides org.eclipse.jetty.util.component.AbstractLifeCycle.doStop.

Method to override to stop the lifecycle
public void
dump(Appendable
The appendable to dump to
out
,
String
The indent to apply after any new lines.
indent
)

Implements org.eclipse.jetty.util.component.Dumpable.dump.

Dump this object (and children) into an Appendable using the provided indent after any new lines.
public void
execute(Runnable
the runnable task
task
)

Overrides default org.eclipse.jetty.util.thread.TryExecutor.execute.

Executes the given command at some time in the future.
public int

Returns:

the number of threads available to tryExecute(Runnable)
getAvailable
()

public int

Returns:

the maximum number of reserved threads
getCapacity
()

public Executor
public long
public int
private static int

Returns:

the number of reserved threads that would be used by a ReservedThreadExecutor constructed with these arguments.
reservedThreads
(Executor
The executor to use to obtain threads
executor
,
int
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.
capacity
)

public void
setIdleTimeout(long
Time to wait before shrinking, or 0 for default timeout.
idleTime
,
TimeUnit
Time units for idle timeout
idleTimeUnit
)

Set the idle timeout for shrinking the reserved thread pool

private void
public String
toString()

Overrides org.eclipse.jetty.util.component.AbstractLifeCycle.toString.

Returns a string representation of the object.
public boolean

Returns:

true if and only if a reserved thread was available and has been assigned the task to run.
tryExecute
(Runnable
the task to run
task
)

Implements org.eclipse.jetty.util.thread.TryExecutor.tryExecute.

Executes the given task if and only if a reserved thread is available.

Inherited from org.eclipse.jetty.util.component.AbstractLifeCycle:
addEventListenergetEventListenersgetStategetStateisFailedisRunningisStartedisStartingisStoppedisStoppingremoveEventListenersetEventListenersstartstop

Field Detail

_capacityback to summary
private final int _capacity
_countback to summary
private final AtomicBiInteger _count
_executorback to summary
private final Executor _executor
_idleTimeNanosback to summary
private long _idleTimeNanos
_lastEmptyNanoTimeback to summary
private final AtomicLong _lastEmptyNanoTime
_leaseback to summary
private ThreadPoolBudget.Lease _lease
_queueback to summary
private final SynchronousQueue<Runnable> _queue
_threadsback to summary
private final Set<ReservedThreadExecutor.ReservedThread> _threads
DEFAULT_IDLE_TIMEOUTback to summary
private static final long DEFAULT_IDLE_TIMEOUT
LOGback to summary
private static final Logger LOG

Hides org.eclipse.jetty.util.component.AbstractLifeCycle.LOG.

STOPback to summary
private static final Runnable STOP

Constructor Detail

ReservedThreadExecutorback to summary
public ReservedThreadExecutor(Executor executor, int capacity)
Parameters
executor:Executor

The executor to use to obtain threads

capacity:int

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

doStartback to summary
public void doStart() throws Exception

Overrides org.eclipse.jetty.util.component.AbstractLifeCycle.doStart.

Doc from org.eclipse.jetty.util.component.AbstractLifeCycle.doStart.

Method to override to start the lifecycle

Annotations
@Override
Exceptions
Exception:
If there was a problem starting. Will cause a transition to FAILED state
doStopback to summary
public void doStop() throws Exception

Overrides org.eclipse.jetty.util.component.AbstractLifeCycle.doStop.

Doc from org.eclipse.jetty.util.component.AbstractLifeCycle.doStop.

Method to override to stop the lifecycle

Annotations
@Override
Exceptions
Exception:
If there was a problem stopping. Will cause a transition to FAILED state
dumpback to summary
public void dump(Appendable out, String indent) throws IOException

Implements org.eclipse.jetty.util.component.Dumpable.dump.

Doc from org.eclipse.jetty.util.component.Dumpable.dump.

Dump 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.

Parameters
out:Appendable

The appendable to dump to

indent:String

The indent to apply after any new lines.

Annotations
@Override
Exceptions
IOException:
if unable to write to Appendable
executeback to summary
public void execute(Runnable task) throws RejectedExecutionException

Overrides default org.eclipse.jetty.util.thread.TryExecutor.execute.

Doc from java.util.concurrent.Executor.execute.

Executes the given command at some time in the future. The command may execute in a new thread, in a pooled thread, or in the calling thread, at the discretion of the Executor implementation.

Parameters
task:Runnable

the runnable task

Annotations
@Override
Exceptions
RejectedExecutionException:
if this task cannot be accepted for execution
getAvailableback to summary
public int getAvailable()
Returns:int

the number of threads available to tryExecute(Runnable)

Annotations
@ManagedAttribute:available reserved threads
readonly:true
getCapacityback to summary
public int getCapacity()
Returns:int

the maximum number of reserved threads

Annotations
@ManagedAttribute:max number of reserved threads
readonly:true
getExecutorback to summary
public Executor getExecutor()
getIdleTimeoutMsback to summary
public long getIdleTimeoutMs()
Annotations
@ManagedAttribute:idle timeout in ms
readonly:true
getPendingback to summary
public int getPending()
Annotations
@ManagedAttribute:pending reserved threads
readonly:true
reservedThreadsback to summary
private static int reservedThreads(Executor executor, int capacity)
Parameters
executor:Executor

The executor to use to obtain threads

capacity:int

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:int

the number of reserved threads that would be used by a ReservedThreadExecutor constructed with these arguments.

setIdleTimeoutback to summary
public void setIdleTimeout(long idleTime, TimeUnit idleTimeUnit)

Set the idle timeout for shrinking the reserved thread pool

Parameters
idleTime:long

Time to wait before shrinking, or 0 for default timeout.

idleTimeUnit:TimeUnit

Time units for idle timeout

startReservedThreadback to summary
private void startReservedThread()
toStringback to summary
public String toString()

Overrides org.eclipse.jetty.util.component.AbstractLifeCycle.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
tryExecuteback to summary
public boolean tryExecute(Runnable task)

Implements org.eclipse.jetty.util.thread.TryExecutor.tryExecute.

Executes the given task if and only if a reserved thread is available.

Parameters
task:Runnable

the task to run

Returns:boolean

true if and only if a reserved thread was available and has been assigned the task to run.

Annotations
@Override
org.eclipse.jetty.util.thread back to summary

private Class ReservedThreadExecutor.ReservedThread

extends Object
implements Runnable
Class Inheritance
  • java.lang.Object
  • org.eclipse.jetty.util.thread.ReservedThreadExecutor.ReservedThread
All Implemented Interfaces
java.lang.Runnable

Field Summary

Modifier and TypeField and Description
private volatile ReservedThreadExecutor.State
private volatile Thread

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private boolean
private Runnable
public void
run()

Implements java.lang.Runnable.run.

Runs this operation.
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

_stateback to summary
private volatile ReservedThreadExecutor.State _state
_threadback to summary
private volatile Thread _thread

Constructor Detail

ReservedThreadback to summary
private ReservedThread()

Method Detail

isReservedback to summary
private boolean isReserved()
reservedWaitback to summary
private Runnable reservedWait()
runback to summary
public void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
org.eclipse.jetty.util.thread back to summary

private final Enum ReservedThreadExecutor.State

extends Enum<ReservedThreadExecutor.State>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final ReservedThreadExecutor.State
public static final ReservedThreadExecutor.State
public static final ReservedThreadExecutor.State
public static final ReservedThreadExecutor.State
public static final ReservedThreadExecutor.State

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static ReservedThreadExecutor.State
public static ReservedThreadExecutor.State[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

IDLEback to summary
public static final ReservedThreadExecutor.State IDLE
PENDINGback to summary
public static final ReservedThreadExecutor.State PENDING
RESERVEDback to summary
public static final ReservedThreadExecutor.State RESERVED
RUNNINGback to summary
public static final ReservedThreadExecutor.State RUNNING
STOPPEDback to summary
public static final ReservedThreadExecutor.State STOPPED

Constructor Detail

Stateback to summary
private State()

Method Detail

valueOfback to summary
public static ReservedThreadExecutor.State valueOf(String name)
valuesback to summary
public static ReservedThreadExecutor.State[] values()