runInterruptible
Calls the specified block with a given coroutine context in an interruptible manner. The blocking code block will be interrupted and this function will throw CancellationException if the coroutine is cancelled.
Example:
withTimeout(500L) { // Cancels coroutine on timeout
runInterruptible { // Throws CancellationException if interrupted
doSomethingBlocking() // Interrupted on coroutines cancellation
}
}There is an optional context parameter to this function working just like withContext. It enables single-call conversion of interruptible Java methods into suspending functions. With one call here we are moving the call to Dispatchers.IO and supporting interruption:
suspend fun <T> BlockingQueue<T>.awaitTake(): T =
runInterruptible(Dispatchers.IO) { queue.take() }runInterruptible uses withContext as an underlying mechanism for switching context, meaning that the supplied block is invoked in an undispatched manner directly by the caller if CoroutineDispatcher from the current coroutineContext is the same as the one supplied in context.
Deprecated
Passing a Job to `runInterruptible` prevents it from being cancelled when the caller gets cancelled. This pattern should be avoided. This overload will be deprecated with an error in the future.
Deprecated version of runInterruptible that accepts a Job.
The purpose of the runInterruptible function is to interrupt the code executing in block when the caller gets cancelled, but passing a Job in context breaks the structured concurrency relationship between the code being invoked and the code running in block.
See the withContext documentation for a description of how to ensure the block gets cancelled when a Job other than that of the calling coroutine gets cancelled.