tree: f512bd871f42064d553341457ecb35a121d404d6 [path history] [tgz]
  1. api/
  2. common/
  3. concurrent/
  4. jdk8/
  5. js/
  6. jsAndWasmJsShared/
  7. jsAndWasmShared/
  8. jvm/
  9. jvmBenchmark/
  10. native/
  11. nativeDarwin/
  12. nativeOther/
  13. wasmJs/
  14. wasmWasi/
  15. build.gradle.kts
  16. knit.properties
  17. README.md
kotlinx-coroutines-core/README.md

Module kotlinx-coroutines-core

Core primitives to work with coroutines.

Coroutine builder functions:

NameResultScopeDescription
launchJobCoroutineScopeLaunches coroutine that does not have any result
asyncDeferredCoroutineScopeReturns a single value with the future result
produceReceiveChannelProducerScopeProduces a stream of elements
runBlockingTCoroutineScopeBlocks the thread while the coroutine runs

Coroutine dispatchers implementing CoroutineDispatcher:

NameDescription
Dispatchers.MainConfines coroutine execution to the UI thread
Dispatchers.DefaultConfines coroutine execution to a shared pool of background threads
Dispatchers.UnconfinedDoes not confine coroutine execution in any way
CoroutineDispatcher.limitedParallelismCreates a view of the given dispatcher, limiting the number of tasks executing in parallel

More context elements:

NameDescription
NonCancellableA non-cancelable job that is always active
CoroutineExceptionHandlerHandler for uncaught exception

Synchronization primitives for coroutines:

NameSuspending functionsDescription
MutexlockMutual exclusion
SemaphoreacquireLimiting the maximum concurrency
Channelsend, receiveCommunication channel (aka queue or exchanger)
FlowcollectAsynchronous stream of values

Top-level suspending functions:

NameDescription
delayNon-blocking sleep
yieldYields thread in single-threaded dispatchers
withContextSwitches to a different context
withTimeoutSet execution time-limit with exception on timeout
withTimeoutOrNullSet execution time-limit will null result on timeout
awaitAllAwaits for successful completion of all given jobs or exceptional completion of any
joinAllJoins on all given jobs

Cancellation support for user-defined suspending functions is available with suspendCancellableCoroutine helper function. The NonCancellable job object is provided to suppress cancellation inside the withContext(NonCancellable) {...} block of code.

Ways to construct asynchronous streams of values:

NameTypeDescription
flowcoldRuns a generator-style block of code that emits values
flowOfcoldEmits the values passed as arguments
channelFlowcoldRuns the given code, providing a channel sending to which means emitting from the flow
callbackFlowcoldAllows transforming a callback-based API into a flow
ReceiveChannel.consumeAsFlowhotTransforms a channel into a flow, emitting all of the received values to a single subscriber
ReceiveChannel.receiveAsFlowhotTransforms a channel into a flow, distributing the received values among its subscribers
MutableSharedFlowhotAllows emitting each value to arbitrarily many subscribers at once
MutableStateFlowhotRepresents mutable state as a flow

A cold stream is some process of generating values, and this process is performed separately for each subscriber. A hot stream uses the same source of values independently of whether there are subscribers.

A select expression waits for the result of multiple suspending functions simultaneously:

ReceiverSuspending functionSelect clauseNon-suspending version
JobjoinonJoinisCompleted
DeferredawaitonAwaitisCompleted
SendChannelsendonSendtrySend
ReceiveChannelreceiveonReceivetryReceive
ReceiveChannelreceiveCatchingonReceiveCatchingtryReceive
nonedelayonTimeoutnone

Package kotlinx.coroutines

General-purpose coroutine builders, contexts, and helper functions.

Package kotlinx.coroutines.sync

Synchronization primitives (mutex and semaphore).

Package kotlinx.coroutines.channels

Channels — non-blocking primitives for communicating a stream of elements between coroutines.

Package kotlinx.coroutines.flow

Flow — asynchronous cold and hot streams of elements.

Package kotlinx.coroutines.selects

Select — expressions that perform multiple suspending operations simultaneously until one of them succeeds.

Package kotlinx.coroutines.intrinsics

Low-level primitives for finer-grained control of coroutines.

Package kotlinx.coroutines.future

JDK 8's CompletableFuture support.

Package kotlinx.coroutines.stream

JDK 8's Stream support.

Package kotlinx.coroutines.time

JDK 8's Duration support via additional overloads for existing time-based operators.