Package-level declarations
General-purpose coroutine builders, contexts, and helper functions.
General-purpose coroutine builders, contexts, and helper functions.
General-purpose coroutine builders, contexts, and helper functions.
General-purpose coroutine builders, contexts, and helper functions.
General-purpose coroutine builders, contexts, and helper functions.
Types
Cancellable continuation. It is completed when resumed or cancelled. When the cancel function is explicitly invoked, this continuation immediately resumes with a CancellationException or the specified cancel cause.
CoroutineDispatcher that provides a method to close it, causing the rejection of any new tasks and cleanup of all underlying resources associated with the current dispatcher. Examples of closeable dispatchers are dispatchers backed by java.lang.Executor
and by kotlin.native.Worker
.
A job that can be completed using complete() function. It is returned by Job() and SupervisorJob() constructor functions.
Handler for Job.invokeOnCompletion and CancellableContinuation.invokeOnCancellation.
Installed handler should not throw any exceptions. If it does, they will get caught, wrapped into CompletionHandlerException, and rethrown, potentially causing crash of unrelated code.
The meaning of cause
that is passed to the handler:
Cause is
null
when the job has completed normally.Cause is an instance of CancellationException when the job was cancelled normally. It should not be treated as an error. In particular, it should not be reported to error logs.
Otherwise, the job had failed.
Note: This type is a part of internal machinery that supports parent-child hierarchies and allows for implementation of suspending functions that wait on the Job's state. This type should not be used in general application code. Implementations of CompletionHandler
must be fast and lock-free.
A ThreadContextElement copied whenever a child coroutine inherits a context containing it.
Throwable which can be cloned during stacktrace recovery in a class-specific way. For additional information about stacktrace recovery see STACKTRACE_RECOVERY_PROPERTY_NAME
Base class to be extended by all coroutine dispatcher implementations.
An optional element in the coroutine context to handle uncaught exceptions.
User-specified name of coroutine. This name is used in debugging mode. See newCoroutineContext for the description of coroutine debugging facilities.
Defines a scope for new coroutines. Every coroutine builder (like launch, async, etc.) is an extension on CoroutineScope and inherits its coroutineContext to automatically propagate all its elements and cancellation.
Marks declarations in the coroutines that are delicate — they have limited use-case and shall be used with care in general code. Any use of a delicate declaration has to be carefully reviewed to make sure it is properly used and does not create problems like memory and resource leaks. Carefully read documentation of any declaration marked as DelicateCoroutinesApi
.
Groups various implementations of CoroutineDispatcher.
Groups various implementations of CoroutineDispatcher.
A handle to an allocated object that can be disposed to make it eligible for garbage collection.
CoroutineDispatcher that has underlying Executor for dispatching tasks. Instances of ExecutorCoroutineDispatcher should be closed by the owner of the dispatcher.
Marks declarations that are still experimental in coroutines API, which means that the design of the corresponding declarations has open issues which may (or may not) lead to their changes in the future. Roughly speaking, there is a chance that those declarations will be deprecated in the near future or the semantics of their behavior may change in some way that may break some code.
Marks Flow-related API as a feature preview.
A global CoroutineScope not bound to any job. Global scope is used to launch top-level coroutines which are operating on the whole application lifetime and are not cancelled prematurely.
Marks declarations that are internal in coroutines API, which means that should not be used outside of kotlinx.coroutines
, because their signatures and semantics will change between future releases without any warnings and without providing any migration aids.
A background job. Conceptually, a job is a cancellable thing with a life-cycle that culminates in its completion.
Base class for special CoroutineDispatcher which is confined to application "Main" or "UI" thread and used for any UI-based activities. Instance of MainDispatcher
can be obtained by Dispatchers.Main.
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.
Marks declarations that are obsolete in coroutines API, which means that the design of the corresponding declarations has serious known flaws and they will be redesigned in the future. Roughly speaking, these declarations will be deprecated in the future but there is no replacement for them yet, so they cannot be deprecated right away.
A runnable task for CoroutineDispatcher.dispatch.
A runnable task for CoroutineDispatcher.dispatch.
A runnable task for CoroutineDispatcher.dispatch.
Defines elements in CoroutineContext that are installed into thread context every time the coroutine with this element in the context is resumed on a thread.
This exception is thrown by withTimeout to indicate timeout.
Functions
Wraps ThreadLocal into ThreadContextElement. The resulting ThreadContextElement maintains the given value of the given ThreadLocal for coroutine regardless of the actual thread its is resumed on. By default ThreadLocal.get is used as a value for the thread-local variable, but it can be overridden with value parameter. Beware that context element does not track modifications of the thread-local and accessing thread-local from coroutine without the corresponding context element returns undefined value. See the examples for a detailed description.
Converts an instance of Window to an implementation of CoroutineDispatcher.
Converts an instance of Executor to an implementation of CoroutineDispatcher.
Converts an instance of ExecutorService to an implementation of ExecutorCoroutineDispatcher.
Converts an instance of CoroutineDispatcher to an implementation of Executor.
Creates a coroutine and returns its future result as an implementation of Deferred. The running coroutine is cancelled when the resulting deferred is cancelled. The resulting coroutine has a key difference compared with similar primitives in other languages and frameworks: it cancels the parent job (or outer scope) on failure to enforce structured concurrency paradigm. To change that behaviour, supervising parent (SupervisorJob or supervisorScope) can be used.
Awaits for completion of given deferred values without blocking a thread and resumes normally with the list of values when all deferred computations are complete or resumes with the first thrown exception if any of computations complete exceptionally including cancellation.
Suspends coroutine until next JS animation frame and returns frame time on resumption. The time is consistent with window.performance.now(). This function is cancellable. If the Job of the current coroutine is completed while this suspending function is waiting, this function immediately resumes with CancellationException.
Suspends until cancellation, in which case it will throw a CancellationException.
Cancels Job of this context with an optional cancellation cause. See Job.cancel for details.
Cancels this scope, including its job and all its children with an optional cancellation cause. A cause can be used to specify an error message or to provide other details on a cancellation reason for debugging purposes. Throws IllegalStateException if the scope does not have a job in it.
Cancels this scope, including its job and all its children with a specified diagnostic error message. A cause can be specified to provide additional details on a cancellation reason for debugging purposes. Throws IllegalStateException if the scope does not have a job in it.
Cancels the job and suspends the invoking coroutine until the cancelled job is complete.
Cancels all children of the Job in this context, without touching the state of this job itself with an optional cancellation cause. See Job.cancel. It does not do anything if there is no job in the context or it has no children.
Cancels all children jobs of this coroutine using Job.cancel for all of them with an optional cancellation cause. Unlike Job.cancel on this job as a whole, the state of this job itself is not affected.
Cancels a specified future when this job is cancelled. This is a shortcut for the following code with slightly more efficient implementation (one fewer object created).
Creates a cancellation exception with a specified message and cause.
Creates an already completedCompletableDeferred with a given value.
Creates a CompletableDeferred in an active state. It is optionally a child of a parent job.
Completes this deferred value with the value or exception in the given result. Returns true
if this deferred was completed as a result of this invocation and false
otherwise (if it was already completed).
Creates a CoroutineExceptionHandler instance.
Creates a CoroutineScope and calls the specified suspend block with this scope. The provided scope inherits its coroutineContext from the outer scope, but overrides the context's Job.
Returns the current CoroutineContext retrieved by using kotlin.coroutines.coroutineContext. This function is an alias to avoid name clash with CoroutineScope.coroutineContext in a receiver position:
Ensures that job in the current context is active.
Ensures that current scope is active.
Ensures that current job is active. If the job is no longer active, throws CancellationException. If the job was cancelled, thrown exception contains the original cancellation cause.
Checks whether current thread local is present in the coroutine context and throws IllegalStateException if it is not. It is a good practice to validate that thread local is present in the context, especially in large code-bases, to avoid stale thread-local values and to have a strict invariants.
Helper function for coroutine builder implementations to handle uncaught and unexpected exceptions in coroutines, that could not be otherwise handled in a normal way through structured concurrency, saving them to a future, and cannot be rethrown. This is a last resort handler to prevent lost exceptions.
Calls the specified suspending block with the given CoroutineDispatcher, suspends until it completes, and returns the result.
Return true
when current thread local is present in the coroutine context, false
otherwise. Thread local can be present in the context only if it was added via asContextElement to the context.
Creates a job object in an active state. A failure of any child of this job immediately causes this job to fail, too, and cancels the rest of its children.
Suspends current coroutine until all given jobs are complete. This method is semantically equivalent to joining all given jobs one by one with forEach { it.join() }
.
Suspends current coroutine until all given jobs are complete. This method is semantically equivalent to joining all given jobs one by one with jobs.forEach { it.join() }
.
Creates the main CoroutineScope for UI components.
Creates a context for a new coroutine. It installs Dispatchers.Default when no other dispatcher or ContinuationInterceptor is specified and adds optional support for debugging facilities (when turned on) and copyable-thread-local facilities on JVM.
Creates a context for a new coroutine. It installs Dispatchers.Default when no other dispatcher or ContinuationInterceptor is specified and adds optional support for debugging facilities (when turned on) and copyable-thread-local facilities on JVM. See DEBUG_PROPERTY_NAME for description of debugging facilities on JVM.
Creates a coroutine execution context with the fixed-size thread-pool and built-in yield support. NOTE: The resulting ExecutorCoroutineDispatcher owns native resources (its threads). Resources are reclaimed by ExecutorCoroutineDispatcher.close.
Creates a coroutine execution context using a single thread with built-in yield support. NOTE: The resulting ExecutorCoroutineDispatcher owns native resources (its thread). Resources are reclaimed by ExecutorCoroutineDispatcher.close.
Adds the specified coroutine context to this scope, overriding existing elements in the current scope's context with the corresponding keys.
Starts new coroutine and returns its result as an implementation of Promise.
Runs a new coroutine and blocks the current thread until its completion. This function should not be used from a coroutine. It is designed to bridge regular blocking code to libraries that are written in suspending style, to be used in main
functions and in tests.
Runs a new coroutine and blocks the current thread interruptibly until its completion. This function should not be used from a coroutine. It is designed to bridge regular blocking code to libraries that are written in suspending style, to be used in main
functions and in tests.
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 main
functions and in tests.
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.
Creates a supervisor job object in an active state. Children of a supervisor job can fail independently of each other.
Creates a CoroutineScope with SupervisorJob and calls the specified suspend block with this scope. The provided scope inherits its coroutineContext from the outer scope, but overrides context's Job with SupervisorJob. This function returns as soon as the given block and all its child coroutines are completed.
Suspends the coroutine like suspendCoroutine, but providing a CancellableContinuation to the block. This function throws a CancellationException if the Job of the coroutine is cancelled or completed while it is suspended.
Calls the specified suspending block with a given coroutine context, suspends until it completes, and returns the result.
Runs a given suspending block of code inside a coroutine with a specified timeout and throws a TimeoutCancellationException if the timeout was exceeded.
Runs a given suspending block of code inside a coroutine with the specified timeout and throws a TimeoutCancellationException if the timeout was exceeded.
Properties
Automatic debug configuration value for DEBUG_PROPERTY_NAME.
Name of the property that defines the maximal number of threads that are used by Dispatchers.IO coroutines dispatcher.
Retrieves the current Job instance from the given CoroutineContext or throws IllegalStateException if no job is present in the context.