Skip to content

okio / okio / Timeout


(js, jvm, native) open 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 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 pre-loading content.


Name Summary
(js, jvm, native) <init> <init>()


Name Summary
(jvm) clearDeadline Clears the fun clearDeadline():Timeout
(jvm) clearTimeout Clears the timeout. Operating system timeouts may still fun clearTimeout():Timeout
(jvm) deadline Set a deadline of now plus duration deadline(duration:Long, unit:TimeUnit):Timeout
(jvm) deadlineNanoTime Returns the nano time when the deadline will be fun deadlineNanoTime():Long
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 fun deadlineNanoTime(deadlineNanoTime:Long):Timeout
(jvm) hasDeadline Returns true if a deadline is fun hasDeadline():Boolean
(jvm) intersectWith Applies the minimum intersection between this timeout and other, run block, then finally rollback this timeout’s intersectWith(other:Timeout, block: () ->Unit):Unit
(jvm) throwIfReached Throws an 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 fun throwIfReached():Unit
(jvm) timeout Wait at most 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 fun timeout(timeout:Long, unit:TimeUnit):Timeout
(jvm) timeoutNanos Returns the timeout in nanoseconds, or 0 for no fun timeoutNanos():Long
(jvm) waitUntilNotified Waits on 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 waitUntilNotified(monitor:Any):Unit

Companion Object Properties

Name Summary
(js, jvm, native) 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.val NONE:Timeout

Companion Object Functions

Name Summary
(jvm) minTimeout fun minTimeout(aNanos:Long, bNanos:Long):Long


Name Summary
(jvm) AsyncTimeout This timeout uses a background thread to take action exactly when the timeout occurs. Use this to implement timeouts where they aren’t supported natively, such as to sockets that are blocked on class AsyncTimeout :Timeout
(jvm) ForwardingTimeout A Timeout which forwards calls to another. Useful for class ForwardingTimeout :Timeout