Core primitives to work with coroutines.
Coroutine builder functions:
Name | Result | Scope | Description |
---|---|---|---|
launch | Job | CoroutineScope | Launches coroutine that does not have any result |
async | Deferred | CoroutineScope | Returns a single value with the future result |
produce | ProducerJob | ProducerScope | Produces a stream of elements |
runBlocking | T | CoroutineScope | Blocks the thread while the coroutine runs |
Coroutine dispatchers implementing CoroutineDispatcher:
Name | Description |
---|---|
CommonPool | Confines coroutine execution to a shared pool of threads |
newSingleThreadContext | Create new single-threaded coroutine context |
newFixedThreadPoolContext | Creates new thread pool of a fixed size |
Executor.toCoroutineDispatcher | Extension to convert any executor |
Unconfined | Does not confine coroutine execution in any way |
Synchronization primitives for coroutines:
Name | Suspending functions | Description |
---|---|---|
Mutex | lock | Mutual exclusion |
Channel | send, receive | Communication channel (aka queue or exchanger) |
Top-level suspending functions:
Name | Description |
---|---|
delay | Non-blocking sleep |
yield | Yields thread in single-threaded dispatchers |
run | Switches to a different context |
withTimeout | Set execution time-limit (deadline) |
Cancellation support for user-defined suspending functions is available with suspendCancellableCoroutine helper function. NonCancellable job object is provided to suppress cancellation with run(NonCancellable) {...}
block of code.
This module provides debugging facilities for coroutines (run JVM with -ea
or -Dkotlinx.coroutines.debug
options) and newCoroutineContext function to write user-defined coroutine builders that work with these debugging facilities.
General-purpose coroutine builders, contexts, and helper functions.
Channels -- non-blocking primitives for communicating a stream of elements between coroutines.