Class YieldingIdleStrategy

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

public final class YieldingIdleStrategy extends Object implements IdleStrategy
IdleStrategy that will call Thread.yield() when the work count is zero.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    Name to be returned from alias().
    As there is no instance state then this object can be used to save on allocation.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a new instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    Simple name by which the strategy can be identified.
    void
    Perform current idle action (e.g. nothing/yield/sleep).
    void
    idle(int workCount)
    Perform current idle action (e.g. nothing/yield/sleep).
    void
    Reset the internal state in preparation for entering an idle state again.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

  • Constructor Details

    • YieldingIdleStrategy

      public YieldingIdleStrategy()
      Create a new instance.
  • Method Details

    • idle

      public void idle(int workCount)
      Perform current idle action (e.g. nothing/yield/sleep). This method signature expects users to call into it on every work 'cycle'. The implementations may use the indication "workCount > 0" to reset internal backoff state. This method works well with 'work' APIs which follow the following rules:
      • 'work' returns a value larger than 0 when some work has been done
      • 'work' returns 0 when no work has been done
      • 'work' may return error codes which are less than 0, but which amount to no work has been done

      Callers are expected to follow this pattern:

      
      while (isRunning)
      {
          idleStrategy.idle(doWork());
      }
      
      
      Specified by:
      idle in interface IdleStrategy
      Parameters:
      workCount - performed in last duty cycle.
    • 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