public class Timeout extends Object
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 any data 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.
| Modifier and Type | Field and Description | 
|---|---|
| static Timeout | NONEAn empty timeout that neither tracks nor detects timeouts. | 
| Constructor and Description | 
|---|
| Timeout() | 
| Modifier and Type | Method and Description | 
|---|---|
| Timeout | clearDeadline()Clears the deadline. | 
| Timeout | clearTimeout()Clears the timeout. | 
| Timeout | deadline(long duration,
        TimeUnit unit)Set a deadline of now plus  durationtime. | 
| long | deadlineNanoTime()Returns the nano time when the deadline will
 be reached. | 
| Timeout | deadlineNanoTime(long deadlineNanoTime)Sets the nano time when the deadline will be
 reached. | 
| boolean | hasDeadline()Returns true if a deadline is enabled. | 
| void | throwIfReached()Throws an  InterruptedIOExceptionif the deadline has been reached or if the current
 thread has been interrupted. | 
| Timeout | timeout(long timeout,
       TimeUnit unit)Wait at most  timeouttime before aborting an operation. | 
| long | timeoutNanos()Returns the timeout in nanoseconds, or  0for no timeout. | 
| void | waitUntilNotified(Object monitor)Waits on  monitoruntil it is notified. | 
public static final Timeout NONE
public Timeout timeout(long timeout, TimeUnit unit)
timeout time before aborting an operation. Using a
 per-operation timeout means that as long as forward progress is being made,
 no sequence of operations will fail.
 If timeout == 0, operations will run indefinitely. (Operating
 system timeouts may still apply.)
public long timeoutNanos()
0 for no timeout.public boolean hasDeadline()
public long deadlineNanoTime()
IllegalStateException - if no deadline is set.public Timeout deadlineNanoTime(long deadlineNanoTime)
public final Timeout deadline(long duration, TimeUnit unit)
duration time.public Timeout clearTimeout()
public Timeout clearDeadline()
public void throwIfReached()
                    throws IOException
InterruptedIOException if the deadline has been reached or if the current
 thread has been interrupted. This method doesn't detect timeouts; that should be implemented to
 asynchronously abort an in-progress operation.IOExceptionpublic final void waitUntilNotified(Object monitor) throws InterruptedIOException
monitor until it is notified. Throws InterruptedIOException if either
 the thread is interrupted or if this timeout elapses before monitor is notified. The
 caller must be synchronized on monitor.
 Here's a sample class that uses waitUntilNotified() to await a specific state. Note
 that the call is made within a loop to avoid unnecessary waiting and to mitigate spurious
 notifications. 
   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);
       }
     }
   }
 InterruptedIOExceptionCopyright © 2017. All rights reserved.