Class FixedRateBarrier
- All Implemented Interfaces:
Serializable
Once a class is constructed with the duration of an interval and the target
per interval, the await() method only releases callers at the
specified number of times per interval. This class is most useful when
the target number per interval exceeds the limits of other approaches
such as java.util.Timer or
java.util.concurrent.ScheduledThreadPoolExecutor. For instance,
this does a good job of ensuring that something happens about 10000 times
per second, but it's overkill to ensure something happens five times per
hour. This does come at a cost. In the worst case, a single thread is
tied up in a loop doing a small amount of computation followed by a
Thread.yield(). Calling Thread.sleep() is not possible because many
platforms sleep for a minimum of 10ms, and all platforms require sleeping
for at least 1ms.
Testing has shown that this class is accurate for a "no-op"
action up to two million per second, which vastly exceeds its
typical use in tools such as searchrate and modrate. This
class is designed to be called by multiple threads, however, it does not
make any fairness guarantee between threads; a single-thread might be
released from the await() method many times before another thread
that is blocked in that method.
This class attempts to smooth out the target per interval throughout each interval. At a given ratio, R between 0 and 1, through the interval, the expected number of actions to have been performed in the interval at that time is R times the target per interval. That is, 10% of the way through the interval, approximately 10% of the actions have been performed, and 80% of the way through the interval, 80% of the actions have been performed.
It's possible to wait for multiple "actions" in one call with
await(int). An example use is rate limiting writing bytes out to
a file. You could configure a FixedRateBarrier to only allow 1M bytes to
be written per second, and then call await(int) with the size of
the byte buffer to write. The call to await(int) would block until
writing out the buffer would not exceed the desired rate.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionFixedRateBarrier(long intervalDurationMs, int perInterval) Constructs a new FixedRateBarrier, which is active untilshutdownRequestedis called. -
Method Summary
Modifier and TypeMethodDescriptionbooleanawait()This method waits until it is time for the next 'action' to be performed based on the specified interval duration and target per interval.booleanawait(int count) This method waits until it is time for the nextcount'actions' to be performed based on the specified interval duration and target per interval.Retrieves information about the current target rate for this barrier.booleanReturnstrueif shutdown has been requested.voidsetRate(long intervalDurationMs, int perInterval) Updates the rates associated with this FixedRateBarrier.voidShuts down this barrier.
-
Constructor Details
-
FixedRateBarrier
Constructs a new FixedRateBarrier, which is active untilshutdownRequestedis called.- Parameters:
intervalDurationMs- The duration of the interval in milliseconds.perInterval- The target number of times thatawait()should return per interval.
-
-
Method Details
-
setRate
Updates the rates associated with this FixedRateBarrier. The new rate will be in effect when this method returns.- Parameters:
intervalDurationMs- The duration of the interval in milliseconds.perInterval- The target number of times thatawait()should return per interval.
-
await
This method waits until it is time for the next 'action' to be performed based on the specified interval duration and target per interval. This method can be called by multiple threads simultaneously. This method returns immediately if shutdown has been requested.- Returns:
trueif shutdown has been requested and false otherwise.
-
await
This method waits until it is time for the nextcount'actions' to be performed based on the specified interval duration and target per interval. To achieve the target rate, it's recommended that on averagecountis small relative toperInterval(and thecountmust not be larger thanperInterval). Acountvalue will not be split across intervals, and due to timing issues, it's possible that acountthat barely fits in the current interval will need to wait until the next interval. If it's not possible to use smaller 'count' values, then increaseperIntervalandintervalDurationMsby the same relative amount. As an example, ifcountis on average 1/10 as big asperInterval, then you can expect to attain 90% of the target rate. IncreasingperIntervalandintervalDurationMsby 10x means that 99% of the target rate can be achieved.This method can be called by multiple threads simultaneously. This method returns immediately if shutdown has been requested.
- Parameters:
count- The number of 'actions' being performed. It must be less than or equal toperInterval, and is recommended to be fairly small relative toperIntervalso that it is easier to achieve the desired rate and exhibit smoother performance.- Returns:
trueif shutdown has been requested and false otherwise.
-
getTargetRate
Retrieves information about the current target rate for this barrier. The value returned will include aLongthat specifies the duration of the current interval in milliseconds and anIntegerthat specifies the number of times that theawait()method should return per interval.- Returns:
- Information about hte current target rate for this barrier.
-
shutdownRequested
Shuts down this barrier. Future calls to await() will return immediately. -
isShutdownRequested
Returnstrueif shutdown has been requested.- Returns:
trueif shutdown has been requested andfalseotherwise.
-