An adaptive execution strategy that uses the Invocable status
of both the task and the current thread to select an optimal strategy
that prioritizes executing the task immediately in the current
producing thread if it can be done so without thread starvation issues.
This strategy selects between the following sub-strategies:
Invocable#invokeNonBlocking(Runnable)
and then continues to produce.The sub-strategy is selected as follows:
Invocable.InvocationType#NON_BLOCKING .Invocable.InvocationType#NON_BLOCKING
and a pending producer thread is available, either because there is already a pending producer
or one is successfully started with TryExecutor#tryExecute(Runnable).Invocable.InvocationType#EITHER and EPC was not selected.Because of the preference for PC mode, on a multicore machine with many
many Invocable. tasks, multiple instances of the strategy may be
required to keep all CPUs on the system busy.
Since the producing thread may be invoked with Invocable#invokeNonBlocking(Runnable)
this allows AdaptiveExecutionStrategys to be efficiently and safely chained: a task
produced by one execution strategy may become itself be a producer in a second execution strategy
(e.g. an IO selector may use an execution strategy to handle multiple connections and each
connection may use a execution strategy to handle multiplexed channels/streams within the connection).
A task containing another AdaptiveExecutionStrategy should identify as
Invocable. so when there are no pending producers threads available to
the first strategy, then it may invoke the second as Invocable..
This avoids starvation as the production on the second strategy can always be executed,
but without the risk that it may block the last available producer for the first strategy.
This strategy was previously named EatWhatYouKill (EWYK) because its preference for a producer to directly consume the tasks that it produces is similar to a hunting proverb that says that a hunter should eat (i.e. consume) what they kill (i.e. produced).
| Modifier and Type | Class and Description |
|---|---|
| private static enum | AdaptiveExecutionStrategy.SubStrategy
The sub-strategies used by the strategy to consume tasks that are produced. |
| Modifier and Type | Field and Description |
|---|---|
| private final LongAdder | |
| private final Executor | |
| private final LongAdder | |
| private final LongAdder | |
| private final LongAdder | |
| private final ExecutionStrategy. | |
| private final AtomicBiInteger | |
| private final TryExecutor | |
| private final Executor | |
| private static final int | IDLE
The production state of the strategy. |
| private static final Logger | |
| private static final int | |
| private static final int |
| Access | Constructor and Description |
|---|---|
| public | AdaptiveExecutionStrategy(ExecutionStrategy.
The producer of tasks to be consumed. producer,The executor to be used for executing producers or consumers, depending on the sub-strategy. executor)
|
| Modifier and Type | Method and Description |
|---|---|
| private boolean | Returns: True if the sub-strategy requires the caller to continue to produce tasks.The task to consume. task, AdaptiveExecutionStrategy.The execution sub-strategy to use to consume the task. subStrategyConsumes a task with a sub-strategy. |
| public void | dispatch()
Implements org. Initiates (or resumes) the task production and consumption. |
| private void | |
| public long | |
| public long | |
| public long | |
| public long | |
| private void | |
| private void | |
| private void | invokeAsNonBlocking(Runnable
The task to run in non-blocking mode. task)Runs a task in non-blocking mode. |
| public boolean | |
| public boolean | |
| public void | produce()
Implements org. Initiates (or resumes) the task production and consumption. |
| private Runnable | Returns: A produced task or null if there were no tasks or a Throwable was thrown.Produces a task, logging any Throwable that may result. |
| public void | |
| public void | |
| private void | |
| private AdaptiveExecutionStrategy. | Returns: The sub-strategy to use for the task.The task to select the strategy for. task, boolean True if the producing thread cannot block. nonBlocking)Selects the execution strategy. |
| public String | toString()
Overrides org. |
| public String | |
| private void | tryProduce(boolean
True if the calling thread was started as a pending producer. wasPending)Tries to become the producing thread and then produces and consumes tasks. |
| _epcMode | back to summary |
|---|---|
| private final LongAdder _epcMode | |
| _executor | back to summary |
|---|---|
| private final Executor _executor | |
| _pcMode | back to summary |
|---|---|
| private final LongAdder _pcMode | |
| _pecMode | back to summary |
|---|---|
| private final LongAdder _pecMode | |
| _picMode | back to summary |
|---|---|
| private final LongAdder _picMode | |
| _producer | back to summary |
|---|---|
| private final ExecutionStrategy. | |
| _state | back to summary |
|---|---|
| private final AtomicBiInteger _state Hides org. | |
| _tryExecutor | back to summary |
|---|---|
| private final TryExecutor _tryExecutor | |
| _virtualExecutor | back to summary |
|---|---|
| private final Executor _virtualExecutor | |
| IDLE | back to summary |
|---|---|
| private static final int IDLE The production state of the strategy. | |
| LOG | back to summary |
|---|---|
| private static final Logger LOG Hides org. | |
| PRODUCING | back to summary |
|---|---|
| private static final int PRODUCING | |
| REPRODUCING | back to summary |
|---|---|
| private static final int REPRODUCING | |
| AdaptiveExecutionStrategy | back to summary |
|---|---|
public AdaptiveExecutionStrategy(ExecutionStrategy.
| |
| consumeTask | back to summary |
|---|---|
| private boolean consumeTask(Runnable task, AdaptiveExecutionStrategy. Consumes a task with a sub-strategy.
| |
| dispatch | back to summary |
|---|---|
| public void dispatch() Implements org. Doc from org. Initiates (or resumes) the task production and consumption. This method guarantees that the task is never run by the thread that called this method. TODO review the need for this (only used by HTTP2 push)
| |
| execute | back to summary |
|---|---|
| private void execute(Runnable task) Executes a task via the
| |
| getEPCTasksConsumed | back to summary |
|---|---|
public long getEPCTasksConsumed()
| |
| getPCTasksConsumed | back to summary |
|---|---|
public long getPCTasksConsumed()
| |
| getPECTasksExecuted | back to summary |
|---|---|
public long getPECTasksExecuted()
| |
| getPICTasksExecuted | back to summary |
|---|---|
public long getPICTasksExecuted()
| |
| getState | back to summary |
|---|---|
| private void getState(StringBuilder builder, long biState) | |
| getString | back to summary |
|---|---|
| private void getString(StringBuilder builder) | |
| invokeAsNonBlocking | back to summary |
|---|---|
| private void invokeAsNonBlocking(Runnable task) Runs a task in non-blocking mode.
| |
| isIdle | back to summary |
|---|---|
public boolean isIdle()
| |
| isUseVirtualThreads | back to summary |
|---|---|
public boolean isUseVirtualThreads()
| |
| produce | back to summary |
|---|---|
| public void produce() Implements org. Doc from org. Initiates (or resumes) the task production and consumption. The produced task may be run by the same thread that called this method.
| |
| produceTask | back to summary |
|---|---|
| private Runnable produceTask() Produces a task, logging any Throwable that may result.
| |
| reset | back to summary |
|---|---|
public void reset()
| |
| run | back to summary |
|---|---|
| public void run() Implements java. Doc from java. Runs this operation.
| |
| runTask | back to summary |
|---|---|
| private void runTask(Runnable task) Runs a Runnable task, logging any thrown exception.
| |
| selectSubStrategy | back to summary |
|---|---|
| private AdaptiveExecutionStrategy. Selects the execution strategy.
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides org. Doc from java. Returns a string representation of the object. | |
| toString | back to summary |
|---|---|
| public String toString(long biState) | |
| tryProduce | back to summary |
|---|---|
| private void tryProduce(boolean wasPending) Tries to become the producing thread and then produces and consumes tasks.
| |
| Modifier and Type | Field and Description |
|---|---|
| public static final AdaptiveExecutionStrategy. | EXECUTE_PRODUCE_CONSUME
Executes a pending producer, consumes produced tasks and races the pending producer to continue producing. |
| public static final AdaptiveExecutionStrategy. | PRODUCE_CONSUME
Consumes produced tasks and continues producing. |
| public static final AdaptiveExecutionStrategy. | PRODUCE_EXECUTE_CONSUME
Executes produced tasks and continues producing. |
| public static final AdaptiveExecutionStrategy. | PRODUCE_INVOKE_CONSUME
Consumes produced tasks as non blocking and continues producing. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static AdaptiveExecutionStrategy. | |
| public static AdaptiveExecutionStrategy. |
| EXECUTE_PRODUCE_CONSUME | back to summary |
|---|---|
| public static final AdaptiveExecutionStrategy. Executes a pending producer, consumes produced tasks and races the pending producer to continue producing. | |
| PRODUCE_CONSUME | back to summary |
|---|---|
| public static final AdaptiveExecutionStrategy. Consumes produced tasks and continues producing. | |
| PRODUCE_EXECUTE_CONSUME | back to summary |
|---|---|
| public static final AdaptiveExecutionStrategy. Executes produced tasks and continues producing. | |
| PRODUCE_INVOKE_CONSUME | back to summary |
|---|---|
| public static final AdaptiveExecutionStrategy. Consumes produced tasks as non blocking and continues producing. | |
| SubStrategy | back to summary |
|---|---|
| private SubStrategy() | |
| valueOf | back to summary |
|---|---|
| public static AdaptiveExecutionStrategy. | |
| values | back to summary |
|---|---|
| public static AdaptiveExecutionStrategy. | |