Package 

Class Timeout


  • 
    public class Timeout
    
                        

    A policy on how much time to spend on a task before giving up. When a task times out, it is left in an unspecified state and should be abandoned. For example, if reading from a source times out, that source should be closed and the read should be retried later. If writing to a sink times out, the same rules apply: close the sink and retry later.

    Timeouts and DeadlinesThis class offers two complementary controls to define a timeout policy.

    Timeouts specify the maximum time to wait for a single operation to complete. Timeouts are typically used to detect problems like network partitions. For example, if a remote peer doesn't return anydata for ten seconds, we may assume that the peer is unavailable.

    Deadlines specify the maximum time to spend on a job, composed of one or more operations. Use deadlines to set an upper bound on the time invested on a job. For example, a battery-conscious app may limit how much time it spends pre-loading content.

    • Constructor Detail

      • Timeout

        Timeout()
    • Method Detail

      • timeout

         Timeout timeout(long timeout, TimeUnit unit)

        Wait at most {@code timeout} time before aborting an operation. Using aper-operation timeout means that as long as forward progress is being made,no sequence of operations will fail.

        If {@code timeout == 0}, operations will run indefinitely. (Operatingsystem timeouts may still apply.)

      • timeoutNanos

         long timeoutNanos()

        Returns the timeout in nanoseconds, or {@code 0} for no timeout.

      • hasDeadline

         boolean hasDeadline()

        Returns true if a deadline is enabled.

      • deadlineNanoTime

         Timeout deadlineNanoTime(long deadlineNanoTime)

        Sets the nano time when the deadline will bereached. All operations must complete before this time. Use a deadline toset a maximum bound on the time spent on a sequence of operations.

      • deadline

         final Timeout deadline(long duration, TimeUnit unit)

        Set a deadline of now plus {@code duration} time.

      • clearTimeout

         Timeout clearTimeout()

        Clears the timeout. Operating system timeouts may still apply.

      • throwIfReached

         void throwIfReached()

        Throws an InterruptedIOException if the deadline has been reached or if the currentthread has been interrupted. This method doesn't detect timeouts; that should be implemented toasynchronously abort an in-progress operation.

      • waitUntilNotified

         final void waitUntilNotified(Object monitor)

        Waits on {@code monitor} until it is notified. Throws InterruptedIOException if eitherthe thread is interrupted or if this timeout elapses before {@code monitor} is notified. Thecaller must be synchronized on {@code monitor}.

        Here's a sample class that uses {@code waitUntilNotified()} to await a specific state. Notethat the call is made within a loop to avoid unnecessary waiting and to mitigate spuriousnotifications.

        {@code * * class Dice { * Random random = new Random(); * int latestTotal; * * public synchronized void roll() { * latestTotal = 2 + random.nextInt(6) + random.nextInt(6); * System.out.println("Rolled " + latestTotal); * notifyAll(); * } * * public void rollAtFixedRate(int period, TimeUnit timeUnit) { * Executors.newScheduledThreadPool(0).scheduleAtFixedRate(new Runnable() { * public void run() { * roll(); * } * }, 0, period, timeUnit); * } * * public synchronized void awaitTotal(Timeout timeout, int total) * throws InterruptedIOException { * while (latestTotal != total) { * timeout.waitUntilNotified(this); * } * } * } * }