Class ControllableIdleStrategy

  • All Implemented Interfaces:
    IdleStrategy

    public final class ControllableIdleStrategy
    extends java.lang.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 Thread.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.
    • Constructor Detail

      • 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 Detail

      • 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:
        IdleStrategy.idle(int)
      • 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 java.lang.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 java.lang.String toString()
        Overrides:
        toString in class java.lang.Object