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)  
    • 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()