tree: 40736ee812a2e23e456eea5f07834a22a7b5dab3 [path history] [tgz]
  1. src/
  2. test/
  3. README.md
kotlinx-coroutines-core/common/README.md

Module kotlinx-coroutines-core

Core primitives to work with coroutines available on all platforms.

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
actorSendChannelActorScopeProcesses a stream of messages

Coroutine dispatchers implementing CoroutineDispatcher:

NameDescription
Dispatchers.DefaultConfines coroutine execution to a shared pool of background threads
Dispatchers.UnconfinedDoes not confine coroutine execution in any way
newSingleThreadContextCreates a single-threaded coroutine context
newFixedThreadPoolContextCreates a thread pool of a fixed size
Executor.asCoroutineDispatcherExtension to convert any executor

More context elements:

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

Synchronization primitives for coroutines:

NameSuspending functionsDescription
MutexlockMutual exclusion
Channelsend, receiveCommunication channel (aka queue or exchanger)

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. NonCancellable job object is provided to suppress cancellation with withContext(NonCancellable) {...} block of code.

Select expression waits for the result of multiple suspending functions simultaneously:

ReceiverSuspending functionSelect clauseNon-suspending version
JobjoinonJoinisCompleted
DeferredawaitonAwaitisCompleted
SendChannelsendonSendoffer
ReceiveChannelreceiveonReceivepoll
ReceiveChannelreceiveOrNullonReceiveOrNullpoll
MutexlockonLocktryLock
nonedelayonTimeoutnone

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. See DEBUG_PROPERTY_NAME for more details.

This module provides a special CoroutineContext type TestCoroutineCoroutineContext that allows the writer of code that contains Coroutines with delays and timeouts to write non-flaky unit-tests for that code allowing these tests to terminate in near zero time. See the documentation for this class for more information.

Package kotlinx.coroutines

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

Package kotlinx.coroutines.flow

Flow -- primitive to work with asynchronous and event-based streams of data.

Package kotlinx.coroutines.sync

Synchronization primitives (mutex).

Package kotlinx.coroutines.channels

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

Package kotlinx.coroutines.selects

Select expression to perform multiple suspending operations simultaneously until one of them succeeds.

Package kotlinx.coroutines.intrinsics

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