Class Timeout


  • public class Timeout
    extends java.lang.Object
    Helper class for managing timeouts during I/O operations.

    The Timeout class provides a simple mechanism for tracking timeouts during I/O operations. It helps with implementing operations that should complete within a specified time limit, such as non-blocking reads with timeouts.

    This class supports both finite timeouts (specified in milliseconds) and infinite timeouts (indicated by zero or negative timeout values). It provides methods for starting the timeout countdown, checking if the timeout has expired, and calculating the remaining time.

    The class is designed to be used in scenarios where multiple I/O operations need to share a single timeout, ensuring that the total time for all operations does not exceed the specified limit.

    Example usage:

     // Create a timeout of 5 seconds
     Timeout timeout = new Timeout(5000);
    
     // Start the timeout countdown
     timeout.start();
    
     // Perform I/O operations, checking for timeout
     while (!timeout.isExpired() && !operationComplete()) {
         // Perform a partial operation with the remaining time
         long remaining = timeout.remaining();
         performPartialOperation(remaining);
     }
     
    • Constructor Summary

      Constructors 
      Constructor Description
      Timeout​(long timeout)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean elapsed()  
      boolean isFinite()  
      boolean isInfinite()  
      static long saturatingAddNanos​(long baseNanos, long deltaNanos)
      Adds a nanoTime base and a non-negative duration, clamping to Long.MAX_VALUE on overflow.
      long timeout()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Timeout

        public Timeout​(long timeout)
    • Method Detail

      • isInfinite

        public boolean isInfinite()
      • isFinite

        public boolean isFinite()
      • elapsed

        public boolean elapsed()
      • timeout

        public long timeout()
      • saturatingAddNanos

        public static long saturatingAddNanos​(long baseNanos,
                                              long deltaNanos)
        Adds a nanoTime base and a non-negative duration, clamping to Long.MAX_VALUE on overflow.