Package kotlinx.coroutines.experimental
General-purpose coroutine builders, contexts, and helper functions.
Types
abstract class AbstractCoroutine<in T> : Abstract base class for implementation of coroutines in coroutine builders. |
|
interface CancellableContinuation<in T> : Cancellable continuation. Its job is completed when it is resumed or cancelled. When cancel function is explicitly invoked, this continuation immediately resumes with CancellationException or with the specified cancel cause. |
|
object CommonPool : CoroutineDispatcher Represents common pool of shared threads as coroutine dispatcher for compute-intensive tasks. |
|
A Deferred that can be completed via public functions complete, completeExceptionally, and cancel. |
|
abstract class CoroutineDispatcher : Base class that shall be extended by all coroutine dispatcher implementations. |
|
interface CoroutineExceptionHandler : Element An optional element on the coroutine context to handle uncaught exceptions. |
|
data class CoroutineName : AbstractCoroutineContextElement User-specified name of coroutine. This name is used in debugging mode. See newCoroutineContext for the description of coroutine debugging facilities. |
|
interface CoroutineScope Receiver interface for generic coroutine builders, so that the code inside coroutine has a convenient access to its coroutineContext and its cancellation status via isActive. |
|
enum class CoroutineStart Defines start option for coroutines builders.
It is used in |
|
interface Deferred<out T> : Job Deferred value is a non-blocking cancellable future. |
|
interface Delay This dispatcher feature is implemented by CoroutineDispatcher implementations that natively support scheduled execution of tasks. |
|
interface DisposableHandle : Registration A handle to an allocated object that can be disposed to make it eligible for garbage collection. |
|
interface EventLoop Implemented by CoroutineDispatcher implementations that have event loop inside and can be asked to process next event from their event queue. |
|
interface Job : Element A background job. Conceptually, a job is a cancellable thing with a simple life-cycle that culminates in its completion. Jobs can be arranged into parent-child hierarchies where cancellation or completion of parent immediately cancels all its children. |
|
object NonCancellable : AbstractCoroutineContextElement, Job A non-cancelable job that is always active. It is designed for withContext function to prevent cancellation of code blocks that need to be executed without cancellation. |
|
object NonDisposableHandle : DisposableHandle No-op implementation of DisposableHandle. |
|
interface Runnable A runnable task for CoroutineDispatcher.dispatch. |
|
class ThreadPoolDispatcher : Dispatches coroutine execution to a thread pool of a fixed size. Instances of this dispatcher are created with newSingleThreadContext and newFixedThreadPoolContext. |
|
object Unconfined : CoroutineDispatcher A coroutine dispatcher that is not confined to any specific thread. It executes initial continuation of the coroutine right here in the current call-frame and let the coroutine resume in whatever thread that is used by the corresponding suspending function, without mandating any specific threading policy. |
Exceptions
open class CancellationException : IllegalStateException |
|
class CompletionHandlerException : RuntimeException This exception gets thrown if an exception is caught while processing CompletionHandler invocation for Job. |
|
class JobCancellationException : CancellationException Thrown by cancellable suspending functions if the Job of the coroutine is cancelled or completed without cause, or with a cause or exception that is not CancellationException (see Job.getCancellationException). |
|
class TimeoutCancellationException : TimeoutException This exception is thrown by withTimeout to indicate timeout. |
Type Aliases
typealias CancellationException = CancellationException Thrown by cancellable suspending functions if the Job of the coroutine is cancelled while it is suspending. |
|
Handler for Job.invokeOnCompletion. |
|
typealias Runnable = Runnable A runnable task for CoroutineDispatcher.dispatch. |
Extensions for External Classes
Properties
val DefaultDispatcher: CoroutineDispatcher This is the default CoroutineDispatcher that is used by all standard builders like launch, async, etc if no dispatcher nor any other ContinuationInterceptor is specified in their context. |
Functions
fun <T> CompletableDeferred( Creates a CompletableDeferred in an active state. It is optionally a child of a parent job. fun <T> CompletableDeferred(value: T): CompletableDeferred<T> Creates an already completed CompletableDeferred with a given value. |
|
fun CoroutineExceptionHandler( Creates new CoroutineExceptionHandler instance. |
|
fun EventLoop( Creates a new event loop that is bound the specified thread (current thread by default) and stops accepting new events when parentJob completes. Every continuation that is scheduled onto this event loop unparks the specified thread via LockSupport.unpark. |
|
Creates a new job object in an active state. It is optionally a child of a parent job. |
|
Converts this deferred value to the instance of Promise. |
|
fun <T> async( Creates new coroutine and returns its future result as an implementation of Deferred. |
|
Cancels the job and suspends invoking coroutine until the cancelled job is complete. |
|
Cancels all children jobs of this coroutine with the given cause using Job.cancel for all of them. Unlike Job.cancel on this job as a whole, the state of this job itself is not affected. |
|
fun Job.cancelFutureOnCompletion( Cancels a specified future when this job is complete. |
|
Delays coroutine for a given time without blocking a thread and resumes it after a specified time. This suspending function is cancellable. If the Job of the current coroutine is cancelled or completed while this suspending function is waiting, this function immediately resumes with CancellationException. |
|
fun Job.disposeOnCompletion( Disposes a specified handle when this job is complete. |
|
fun handleCoroutineException( Helper function for coroutine builder implementations to handle uncaught exception in coroutines. |
|
Suspends coroutine until all children of this job are complete using Job.join for all of them. Unlike Job.join on this job as a whole, it does not wait until this job is complete. |
|
fun launch( Launches new coroutine without blocking current thread and returns a reference to the coroutine as a Job. The coroutine is cancelled when the resulting job is cancelled. |
|
fun newCoroutineContext( Creates context for the new coroutine. It installs DefaultDispatcher when no other dispatcher nor ContinuationInterceptor is specified, and adds optional support for debugging facilities (when turned on). |
|
fun newFixedThreadPoolContext( Creates new coroutine execution context with the fixed-size thread-pool and built-in yield and delay support. NOTE: The resulting ThreadPoolDispatcher owns native resources (its threads).Resources are reclaimed by ThreadPoolDispatcher.close. |
|
fun newSingleThreadContext( Creates new coroutine execution context with the a single thread and built-in yield and delay support. NOTE: The resulting ThreadPoolDispatcher owns native resources (its thread).Resources are reclaimed by ThreadPoolDispatcher.close. |
|
fun <T> promise( Starts new coroutine and returns its result as an implementation of Promise. |
|
fun <T> runBlocking( Runs new coroutine and blocks current thread interruptibly until its completion.
This function should not be used from coroutine. It is designed to bridge regular blocking code
to libraries that are written in suspending style, to be used in |
|
suspend fun <T> suspendAtomicCancellableCoroutine( Suspends coroutine similar to suspendCancellableCoroutine, but with atomic cancellation. |
|
suspend fun <T> suspendCancellableCoroutine( Suspends coroutine similar to suspendCoroutine, but provide an implementation of CancellableContinuation to the block. This function throws CancellationException if the coroutine is cancelled or completed while suspended. |
|
suspend fun <T> withContext( Calls the specified suspending block with a given coroutine context, suspends until it completes, and returns the result. |
|
suspend fun <T> withTimeout( suspend fun <T> withTimeout( Runs a given suspending block of code inside a coroutine with a specified timeout and throws TimeoutCancellationException if timeout was exceeded. |
|
suspend fun <T> withTimeoutOrNull( suspend fun <T> withTimeoutOrNull( Runs a given suspending block of code inside a coroutine with a specified timeout and returns
|
|
suspend fun yield(): Unit Yields a thread (or thread pool) of the current coroutine dispatcher to other coroutines to run. If the coroutine dispatcher does not have its own thread pool (like Unconfined dispatcher) then this function does nothing, but checks if the coroutine Job was completed. This suspending function is cancellable. If the Job of the current coroutine is cancelled or completed when this suspending function is invoked or while this function is waiting for dispatching, it resumes with CancellationException. |