public interface Pauser
Pauser objects, each offering different strategies for managing thread execution.
The Pauser is designed to offer flexible pausing strategies depending on CPU availability and desired execution patterns.
This interface also defines the methods for managing pause states and conditions within an application's threading model. It includes methods to pause, unpause, reset, and other utilities that influence thread scheduling and execution behaviors.
Refer to PauserMode for capturing these configurations in a serializable manner.
| Modifier and Type | Interface and Description |
|---|---|
static class |
Pauser.SleepyWarning |
| Modifier and Type | Field and Description |
|---|---|
static boolean |
BALANCED |
static int |
MIN_BUSY |
static int |
MIN_PROCESSORS |
static boolean |
SLEEPY |
| Modifier and Type | Method and Description |
|---|---|
default void |
asyncPause()
Pauses "asynchronously" whereby the issuing EventHandler can
pause without blocking other handlers in the EventLoop.
|
default boolean |
asyncPausing()
Checks if the pauser is currently in an asynchronous pause state.
|
static TimingPauser |
balanced()
A balanced pauser which tries to be busy for short bursts but backs off when idle.
|
static TimingPauser |
balancedUpToMillis(int millis)
A balanced pauser which tries to be busy for short bursts but backs off when idle with a limit of max back off.
|
static @NotNull Pauser |
busy()
Creates a
Pauser that actively keeps the thread busy and does not employ any waiting strategies. |
long |
countPaused()
Returns the number of times the pauser has checked for
completion.
|
static boolean |
getBalanced() |
static boolean |
getSleepy() |
default boolean |
isBusy() |
static MilliPauser |
millis(int millis)
Creates a
MilliPauser that waits for a fixed duration before resuming execution. |
static Pauser |
millis(int minMillis,
int maxMillis)
Creates a
Pauser that pauses with a back-off strategy, starting at a minimum millisecond interval and potentially increasing to a maximum. |
void |
pause()
Pauses the current thread.
|
default void |
pause(long timeout,
TimeUnit timeUnit)
use
TimingPauser.pause(long, TimeUnit) instead |
void |
reset()
Resets the pauser's internal state back (if any) to the most aggressive setting.
|
static TimingPauser |
sleepy()
A sleepy pauser which yields for a millisecond, then sleeps for 1 to 20 ms
|
static @NotNull TimingPauser |
timedBusy()
Creates a
TimingPauser that keeps the thread busy but also incorporates timed waits. |
long |
timePaused()
Returns the paused time so far in milliseconds.
|
void |
unpause()
Try to cancel the pausing if it is pausing.
|
static Pauser |
yielding()
Provides a simple
Pauser that is more process-friendly by yielding the thread execution. |
static Pauser |
yielding(int minBusy)
Returns a
Pauser that either yields, pauses, or does not wait at all, based on system capabilities. |
static final int MIN_PROCESSORS
static final boolean BALANCED
static final boolean SLEEPY
static final int MIN_BUSY
static boolean getBalanced()
static boolean getSleepy()
static Pauser yielding(int minBusy)
Pauser that either yields, pauses, or does not wait at all, based on system capabilities.
It selects the most appropriate pauser based on CPU availability and specified minimal busyness.minBusy - the minimal busyness period in microseconds before yielding or pausingPauserstatic TimingPauser sleepy()
TimingPauser implementing a sleepy strategystatic TimingPauser balanced()
TimingPauser implementing a balanced strategystatic TimingPauser balancedUpToMillis(int millis)
millis - the maximum back-off period in millisecondsTimingPauser implementing a balanced strategy with a maximum back-off limitstatic MilliPauser millis(int millis)
MilliPauser that waits for a fixed duration before resuming execution.millis - the fixed wait time in millisecondsMilliPauserstatic Pauser millis(int minMillis, int maxMillis)
Pauser that pauses with a back-off strategy, starting at a minimum millisecond interval and potentially increasing to a maximum.minMillis - the starting minimum pause duration in millisecondsmaxMillis - the maximum pause duration in millisecondsPauser with a back-off strategystatic Pauser yielding()
Pauser that is more process-friendly by yielding the thread execution.Pauser@NotNull static @NotNull Pauser busy()
Pauser that actively keeps the thread busy and does not employ any waiting strategies.Pauser that never waits@NotNull static @NotNull TimingPauser timedBusy()
TimingPauser that keeps the thread busy but also incorporates timed waits.TimingPauser that combines busy and timed wait strategiesvoid pause()
The actual pause time and thread scheduling impact is not specified and depends on the implementing class. For some implementations, a progressive increase of the pause time is employed, thread executions may or may not be yielded, whereas other implementations may not pause or yield at all.
Thus, depending on the implementation this could do nothing (busy spin), yield, sleep, ...
Call this if no work was done.
default void asyncPause()
The issuing EventHandler can check if it is still pausing
asynchronously by invoking asyncPausing(). Typically, this is
done as depicted below:
// @Override
public boolean action() throws InvalidEventHandlerException {
if (pauser.asyncPausing()) {
// Yield, so that other EventHandlers can run
return false;
}
}
asyncPausing()default boolean asyncPausing()
true if the pauser is still pausing asynchronously, false otherwisevoid reset()
Pausers that progressively increases the pause time are reset back to its lowest pause time.
Call this if you just did some work.
default void pause(long timeout,
TimeUnit timeUnit)
throws TimeoutException
TimingPauser.pause(long, TimeUnit) insteadTimeoutExceptionvoid unpause()
No guarantee is made that this call will actually have an effect.
long timePaused()
long countPaused()
default boolean isBusy()
Copyright © 2024. All rights reserved.