Class 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 the reset() method).
    • Field Detail

      • NEVER

        public static final Timeout NEVER
        Specifies a timeout which never expires; that is, an infinite period of time.
      • IMMEDIATE

        public static final Timeout IMMEDIATE
        Specifies a timeout which is always expired; that is, no period of time at all.
    • 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 unit
        unit - 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 the Object.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 invoke Object#wait()
        Throws:
        InterruptedException - if the underlying Object#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 of Timeout object in loops without requiring constant creation and destruction of objects.
        Returns:
        this timeout recalculated relative to the current time