Package okio
Class Timeout
- java.lang.Object
-
- okio.Timeout
-
- Direct Known Subclasses:
AsyncTimeout,ForwardingTimeout
public class Timeout extends Object
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 Deadlines
This 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 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 preloading content.
-
-
Constructor Summary
Constructors Constructor Description Timeout()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description TimeoutclearDeadline()Clears the deadline.TimeoutclearTimeout()Clears the timeout.Timeoutdeadline(long duration, TimeUnit unit)Set a deadline of now plusdurationtime.longdeadlineNanoTime()Returns the nano time when the deadline will be reached.TimeoutdeadlineNanoTime(long deadlineNanoTime)Sets the nano time when the deadline will be reached.booleanhasDeadline()Returns true if a deadline is enabled.voidthrowIfReached()Throws anInterruptedIOExceptionif the deadline has been reached or if the current thread has been interrupted.Timeouttimeout(long timeout, TimeUnit unit)Wait at mosttimeouttime before aborting an operation.longtimeoutNanos()Returns the timeout in nanoseconds, or0for no timeout.
-
-
-
Field Detail
-
NONE
public static final Timeout NONE
An empty timeout that neither tracks nor detects timeouts. Use this when timeouts aren't necessary, such as in implementations whose operations do not block.
-
-
Method Detail
-
timeout
public Timeout timeout(long timeout, TimeUnit unit)
Wait at mosttimeouttime 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.)
-
timeoutNanos
public long timeoutNanos()
Returns the timeout in nanoseconds, or0for no timeout.
-
hasDeadline
public boolean hasDeadline()
Returns true if a deadline is enabled.
-
deadlineNanoTime
public long deadlineNanoTime()
Returns the nano time when the deadline will be reached.- Throws:
IllegalStateException- if no deadline is set.
-
deadlineNanoTime
public Timeout deadlineNanoTime(long deadlineNanoTime)
Sets the nano time when the deadline will be reached. All operations must complete before this time. Use a deadline to set a maximum bound on the time spent on a sequence of operations.
-
deadline
public final Timeout deadline(long duration, TimeUnit unit)
Set a deadline of now plusdurationtime.
-
clearTimeout
public Timeout clearTimeout()
Clears the timeout. Operating system timeouts may still apply.
-
clearDeadline
public Timeout clearDeadline()
Clears the deadline.
-
throwIfReached
public void throwIfReached() throws IOExceptionThrows anInterruptedIOExceptionif 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.- Throws:
IOException
-
-