- java.lang.Object
-
- com.pervasive.datarush.commons.util.Timeout
-
public class Timeout extends Object
A representation of a point-in-time when a timeout period expires. While expressed as a duration, these always represent specific instances of timeouts; the timeout is relative to the time at which it was created (although it can be recalculated with thereset()
method).
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
hasExpired()
Indicates whether the timeout period has elapsed.static Timeout
millis(long duration)
Creates a timeout expiring in the specified number of milliseconds from now.long
remainingTime()
Gets the amount of time remaining before the timeout expires.Timeout
reset()
Resets the timeout to the full duration with which it was created.static Timeout
seconds(long duration)
Creates a timeout expiring in the specified number of seconds from now.static Timeout
timeout(long duration, TimeUnit unit)
Creates a timeout expiring in the specified duration from now.void
waitOn(Object object)
Waits on the specified object using theObject.wait()
method, honoring the timeout's expiration time.
-
-
-
Method Detail
-
millis
public static Timeout millis(long duration)
Creates a timeout expiring in the specified number of milliseconds from now.- Parameters:
duration
- the length of the timeout, specified in milliseconds- Returns:
- the requested timeout period
- Throws:
IllegalArgumentException
- if the duration is non-positive.
-
timeout
public static Timeout timeout(long duration, TimeUnit unit)
Creates a timeout expiring in the specified duration from now.- Parameters:
duration
- the length of the timeout, in the given unitunit
- the time unit- Returns:
- the specified timeout
- Throws:
IllegalArgumentException
- if the duration is non-positive.
-
seconds
public static Timeout seconds(long duration)
Creates a timeout expiring in the specified number of seconds from now.- Parameters:
duration
- the length of the timeout, specified in seconds- Returns:
- the requested timeout period
- Throws:
IllegalArgumentException
- if the duration is non-positive.
-
hasExpired
public boolean hasExpired()
Indicates whether the timeout period has elapsed.- Returns:
- true if the timeout has expired, false otherwise.
-
remainingTime
public long remainingTime()
Gets the amount of time remaining before the timeout expires.- Returns:
- the time remaining until expiration, in milliseconds.
If the timeout has expired,
0
is returned; if the timeout can never expire,Long.MAX_VALUE
is returned.
-
waitOn
public void waitOn(Object object) throws InterruptedException
Waits on the specified object using theObject.wait()
method, honoring the timeout's expiration time. If the timeout has expired, no wait will be performed.The object's monitor must already be held when invoking this method.
- Parameters:
object
- the object on which to invokeObject#wait()
- Throws:
InterruptedException
- if the underlyingObject#wait()
is interrupted.IllegalMonitorStateException
- if the object's monitor is not held.
-
reset
public Timeout reset()
Resets the timeout to the full duration with which it was created. This permits the reuse ofTimeout
object in loops without requiring constant creation and destruction of objects.- Returns:
- this timeout recalculated relative to the current time
-
-