Class AdjustableCallback
java.lang.Object
com.tccc.kos.commons.util.concurrent.BaseCallback
com.tccc.kos.commons.util.concurrent.AdjustableCallback
Class that supports single (one-shot) or recurring callbacks, allows the delay
to be adjusted while the timer is running, and has variance support to add
a degree of randomness to the timer. This class guarantees that the callback
is never called concurrently, even if a timer is stopped and restarted or
called inline while an existing callback is being performed.
A timer is started as either recurring or non-recurring, and recurring mode can be toggled while the timer is running. Once the timer fires in non-recurring mode, it never fires again. However, if the timer is then re-started, the current recurring mode remains in effect.
When setting the delay for a timer, there are two methods that behave slightly differently:
-
setDelay()
: If the timer is not running, the delay value is used as the new delay value. If the timer is running, then the amount of wait time on the current timer is deducted from the delay for the first cycle of the timer. The intention is that if a timer was set to 60 minutes and the timer has already waited 45 minutes and the delay is then changed to 15 minutes, the timer has already waited long enough to trigger from a real world perspective. This is typically desirable behavior so that QA can shorten timers for testing and not have to wait excessively for the first cycle. This is also useful for operations when adjusting settings on live equipment. -
setAbsDelay()
: The delay is always treated as absolute.
There are two properties that control variability of the effective delay:
-
minVariance
: This is the lower bound on the variance and must be less than or equal tomaxVariance
. This defaults to 1.0 and can be adjusted to the desired lower limit. For example, to allow the effective timer to be up to 25% shorter than the specified delay, the value can be set to 0.75 (75% of specified delay). -
maxVariance
: This is the upper bound on the variance and must be greater than or equal tominVariance
. This defaults to 1.0 and can be adjusted to the desired upper limit. For example, to allow the effective timer to be up to 25% longer than the specified delay, the value can be set to 1.25 (125% of the specified delay).
- Since:
- 1.0
- Version:
- 2022-10-12
-
Constructor Summary
ConstructorsConstructorDescriptionAdjustableCallback
(boolean recurring, int delay, Runnable callback) Creates a timer with the specifiedcallback
method,delay
, andrecurring
flag.AdjustableCallback
(boolean recurring, Runnable callback) Creates a timer with the specifiedcallback
method andrecurring
flag. -
Method Summary
Modifier and TypeMethodDescriptionint
getDelay()
Returns the timer's delay.double
Retrieves the maximum delay variance.double
Retrieves the minimum delay variance.boolean
Indicates if this is a recurring timer or not.setAbsDelay
(int delay) Sets the delay for the timer, regardless of how much time is left on the current timer.setDelay
(int delay) Sets a new delay value for the timer.setMaxVariance
(double maxVariance) Sets the maximum delay variance.setMinVariance
(double minVariance) Sets the minimum delay variance.setRecurring
(boolean recurring) Sets therecurring
flag.start()
Starts the timer.Methods inherited from class com.tccc.kos.commons.util.concurrent.BaseCallback
cancel, flush, flushInline, force, forceInline, getCallback, getWaitTime, isRunning, isStopped
-
Constructor Details
-
AdjustableCallback
Creates a timer with the specifiedcallback
method andrecurring
flag. The timer does not automatically start. Instead, it waits until the calling code calls thestart()
method.- Parameters:
recurring
-true
for a recurring timer,false
for a one-shot timercallback
- the method called when the timer fires
-
AdjustableCallback
Creates a timer with the specifiedcallback
method,delay
, andrecurring
flag. The timer starts immediately.- Parameters:
recurring
-true
for a recurring timer,false
for a one-shot timerdelay
- the initial delay for the timercallback
- the method called when the timer fires
-
-
Method Details
-
isRecurring
public boolean isRecurring()Indicates if this is a recurring timer or not.- Returns:
true
if this a recurring timer,false
if a one-shot timer
-
setRecurring
Sets therecurring
flag.- Parameters:
recurring
-true
to make this timer recurring, orfalse
to make it a one-shot timer
-
getDelay
public int getDelay()Returns the timer's delay.- Returns:
- the delay time (msec)
-
setDelay
Sets a new delay value for the timer.- If the timer is already running, then the amount of time already waited is subtracted from the new delay. The timer is restarted with this new delay.
- If the timer is not running, then only the delay value is updated; the timer is not started.
setAbsDelay()
.- Parameters:
delay
- the delay (msec)
-
getMinVariance
public double getMinVariance()Retrieves the minimum delay variance.- Returns:
- the min delay variance
-
setMinVariance
Sets the minimum delay variance.- Parameters:
minVariance
- the min variance, any non-negative number
-
getMaxVariance
public double getMaxVariance()Retrieves the maximum delay variance.- Returns:
- the max delay variance
-
setMaxVariance
Sets the maximum delay variance.- Parameters:
maxVariance
- the max variance, any non-negative number
-
setAbsDelay
Sets the delay for the timer, regardless of how much time is left on the current timer.- If the timer is currently running, then it is stopped and restarted.
- If the timer is currently stopped, it remains stopped.
- Parameters:
delay
- the delay (msec)
-
start
Starts the timer.- If the timer is currently stopped, then it is started.
- If the timer is currently running, then this call has no effect.
-