Class DelayCalculators

java.lang.Object
space.arim.omnibus.util.concurrent.DelayCalculators

public final class DelayCalculators extends Object
Provides default implementations for DelayCalculator, which determines delays for EnhancedExecutor's repeated scheduling methods.

Fixed delay: fixedDelay()
Fixed rate: fixedRate()
Variable rate: variableRate(LongUnaryOperator)
Author:
A248
  • Method Details

    • fixedDelay

      public static DelayCalculator fixedDelay()
      Schedules at a constant delay between executions.
      Returns:
      a delay function which yields the same delay
    • fixedRate

      public static DelayCalculator fixedRate()
      Schedules at a fixed rate of delay, meaning execution initiates at constant intervals.

      Attempts to compensate for execution time by subtracting it from the next interval, in order to achieve a fixed rate of scheduling provided execution time is usually less than the rate.

      For example, if the fixed rate is 4 seconds, an execution timeline might be as follows:
      |--- d ----| |E| |- d -| |--E--| |- d -|
      |- 4 secs -| | 4 secs | |-- 4 secs --|
      where E represents an execution and d is a period of delay.

      Should execution time take longer than the fixed rate of delay, the function will:
      1. Return a zero (nonexistent) delay to immediately commence the next execution. 2. Temporarily accelerate future executions in order to "correct" any waverings from the fixed rate schedule. Thus, if a single execution takes a long time, it will not permanently ruin the rate
      Returns:
      a delay function which yields the same rate of delay
    • variableRate

      public static DelayCalculator variableRate(LongUnaryOperator rateFunction)
      A more complicated version of fixedRate(). Execution initiates at intervals determined by the rate function, which acts on time units in nanoseconds.

      Attempts to compensate for execution time in calculating the next delay. The goal is to achieve the rate of scheduling determined by the latest application of the rate function.

      Should execution time take longer than rate calculated, the function will:
      1. Return a zero (nonexistent) delay to immediately commence the next execution. 2. Temporarily accelerate future executions in order to "correct" any waverings from the rates desired. Thus, if a single execution takes a long time, it will not permanently ruin the rate.

      The rate function takes the last rate, in nanoseconds, and calculates the next rate, also in nanoseconds.
      Parameters:
      rateFunction - the rate determining function
      Returns:
      a delay function which yields a rate of delay as specified by the rate function