Class BackoffIdleStrategy

  • All Implemented Interfaces:
    IdleStrategy

    public final class BackoffIdleStrategy
    extends BackoffIdleStrategyData
    implements IdleStrategy
    Idling strategy for threads when they have no work to do.

    Spin for maxSpins, then Thread.yield() for maxYields, then LockSupport.parkNanos(long) on an exponential backoff to maxParkPeriodNs.

    Under Linux, multiple timer events will be coalesced in a 50 us window to minimize the timer overhead on the CPU. E.g. if you want to wait 10 us, it could be you need to wait 50us. This situation can be improved by changing the value of the timerslack_ns property which defaults to 50000. This can be done like this: echo 10000 > /proc/PID/timerslack_ns This will set the timer slack to 10 microseconds for the given PID of the thread. This property can't be set at the process level, so needs to be set for each thread specifically. Also, it isn't guaranteed that after setting the property, the waiting time will be respected.

    • Field Detail

      • DEFAULT_MAX_SPINS

        public static final long DEFAULT_MAX_SPINS
        Default number of times the strategy will spin without work before going to next state.
        See Also:
        Constant Field Values
      • DEFAULT_MAX_YIELDS

        public static final long DEFAULT_MAX_YIELDS
        Default number of times the strategy will yield without work before going to next state.
        See Also:
        Constant Field Values
      • DEFAULT_MIN_PARK_PERIOD_NS

        public static final long DEFAULT_MIN_PARK_PERIOD_NS
        Default minimum interval the strategy will park a thread.
        See Also:
        Constant Field Values
      • DEFAULT_MAX_PARK_PERIOD_NS

        public static final long DEFAULT_MAX_PARK_PERIOD_NS
        Default maximum interval the strategy will park a thread.
        See Also:
        Constant Field Values
      • p064

        byte p064
      • p065

        byte p065
      • p066

        byte p066
      • p067

        byte p067
      • p068

        byte p068
      • p069

        byte p069
      • p070

        byte p070
      • p071

        byte p071
      • p072

        byte p072
      • p073

        byte p073
      • p074

        byte p074
      • p075

        byte p075
      • p076

        byte p076
      • p077

        byte p077
      • p078

        byte p078
      • p079

        byte p079
      • p080

        byte p080
      • p081

        byte p081
      • p082

        byte p082
      • p083

        byte p083
      • p084

        byte p084
      • p085

        byte p085
      • p086

        byte p086
      • p087

        byte p087
      • p088

        byte p088
      • p089

        byte p089
      • p090

        byte p090
      • p091

        byte p091
      • p092

        byte p092
      • p093

        byte p093
      • p094

        byte p094
      • p095

        byte p095
      • p096

        byte p096
      • p097

        byte p097
      • p098

        byte p098
      • p099

        byte p099
      • p100

        byte p100
      • p101

        byte p101
      • p102

        byte p102
      • p103

        byte p103
      • p104

        byte p104
      • p105

        byte p105
      • p106

        byte p106
      • p107

        byte p107
      • p108

        byte p108
      • p109

        byte p109
      • p110

        byte p110
      • p111

        byte p111
      • p112

        byte p112
      • p113

        byte p113
      • p114

        byte p114
      • p115

        byte p115
      • p116

        byte p116
      • p117

        byte p117
      • p118

        byte p118
      • p119

        byte p119
      • p120

        byte p120
      • p121

        byte p121
      • p122

        byte p122
      • p123

        byte p123
      • p124

        byte p124
      • p125

        byte p125
      • p126

        byte p126
      • p127

        byte p127
    • Constructor Detail

      • BackoffIdleStrategy

        public BackoffIdleStrategy​(long maxSpins,
                                   long maxYields,
                                   long minParkPeriodNs,
                                   long maxParkPeriodNs)
        Create a set of state tracking idle behavior.
        Parameters:
        maxSpins - to perform before moving to Thread.yield()
        maxYields - to perform before moving to LockSupport.parkNanos(long)
        minParkPeriodNs - to use when initiating parking
        maxParkPeriodNs - to use when parking
    • Method Detail

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