public class Wait extends Object
Instead of using Thread.sleep(long)
there methods provide a literal feedback on the intended period. Also
we take care of the InterruptedException by ignoring it. This make the code more compact and readable.
Modifier and Type | Method and Description |
---|---|
static void |
duration(Duration durationToWait)
Waits the given amount of time.
|
static void |
millis(int millisToWait)
Waits the given amount of milliseconds.
|
static void |
randomMillis(int minWaitMillis,
int maxWaitMillis)
Waits for an random amount of millisecond within the given bounds.
|
static void |
randomSeconds(double minWaitSeconds,
double maxWaitSeconds)
Waits for a random amount of seconds within the given bounds.
|
static void |
seconds(double secondsToWait)
Waits the given amount of seconds.
|
public static void millis(int millisToWait)
If the given value is 0 or negative, the method returns immediately. If an InterruptedException is thrown while waiting, the method will return immediately but ignore the exception.
millisToWait
- the number of milliseconds to waitpublic static void duration(@Nullable Duration durationToWait)
If the given value is null, the method returns immediately. If an InterruptedException is thrown while waiting, the method will return immediately but ignore the exception.
durationToWait
- the duration to waitpublic static void seconds(double secondsToWait)
If the given value is 0 or negative, the method returns immediately. If an InterruptedException is thrown while waiting, the method will return immediately but ignore the exception.
secondsToWait
- the number of seconds to waitpublic static void randomMillis(int minWaitMillis, int maxWaitMillis)
Note that minWaitMillis may be negative. This can be used to only block the thread in a given percentage of
all calls. So if the thread should wait between 0 and 500ms in 50% of all calls
randomMillis(-500, 500)
can be invoked. Using this bounds, the expected wait time will be
between -500ms and 0ms in 50% of all calls (on average). As negative delays are ignored, the method
will return immediately.
minWaitMillis
- the minimal time to wait in millis.maxWaitMillis
- the maximal time to wait in millis. This must be >= minWaitMillis and also > 0 or the
method will always return immediatelypublic static void randomSeconds(double minWaitSeconds, double maxWaitSeconds)
See randomMillis(int, int)
for a detailed description on using a negative lower bound.
minWaitSeconds
- the minimal time to wait in seconds.maxWaitSeconds
- the maximal time to wait in seconds. This must be >= minWaitSeconds and also > 0
or the
method will always return immediatelyCopyright © 2018. All rights reserved.