Class ControllableIdleStrategy

java.lang.Object
org.agrona.concurrent.ControllableIdleStrategy
All Implemented Interfaces:
IdleStrategy

public final class ControllableIdleStrategy extends Object implements IdleStrategy
IdleStrategy which can be controlled by a counter so its mode of operation can be switched between doing nothing (NOOP), busy spinning by calling ThreadHints.onSpinWait(), yielding by calling Thread.yield(), or sleeping for the minimum period by calling LockSupport.parkNanos(long) when work count is zero, so it idles.
  • Field Details

  • Constructor Details

    • ControllableIdleStrategy

      public ControllableIdleStrategy(StatusIndicatorReader statusIndicator)
      Configure idle strategy taking an indicator of which strategy should be applied.
      Parameters:
      statusIndicator - indicator of which strategy should be applied.
  • Method Details

    • idle

      public void idle(int workCount)
      Idle based on current status indication value
      Specified by:
      idle in interface IdleStrategy
      Parameters:
      workCount - performed in last duty cycle.
      See Also:
    • idle

      public void idle()
      Perform current idle action (e.g. nothing/yield/sleep). To be used in conjunction with IdleStrategy.reset() to clear internal state when idle period is over (or before it begins). Callers are expected to follow this pattern:
       
       while (isRunning)
       {
         if (!hasWork())
         {
           idleStrategy.reset();
           while (!hasWork())
           {
             if (!isRunning)
             {
               return;
             }
             idleStrategy.idle();
           }
         }
         doWork();
       }
       
       
      Specified by:
      idle in interface IdleStrategy
    • reset

      public void reset()
      Reset the internal state in preparation for entering an idle state again.
      Specified by:
      reset in interface IdleStrategy
    • alias

      public String alias()
      Simple name by which the strategy can be identified.
      Specified by:
      alias in interface IdleStrategy
      Returns:
      simple name by which the strategy can be identified.
    • toString

      public String toString()
      Overrides:
      toString in class Object