- Notifications
You must be signed in to change notification settings - Fork1.9k
Library support for Kotlin coroutines
License
Kotlin/kotlinx.coroutines
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Library support for Kotlin coroutines withmultiplatform support.This is a companion version for the Kotlin2.0.0
release.
suspendfunmain()= coroutineScope { launch { delay(1000)println("Kotlin Coroutines World!") }println("Hello")}
Play with coroutines onlinehere
- core — common coroutines across all platforms:
- launch andasync coroutine builders returningJob andDeferred light-weight futures with cancellation support;
- Dispatchers object withMain dispatcher for Android/Swing/JavaFx (which require the corresponding artifacts in runtime) and Darwin (included out of the box), andDefault dispatcher for background coroutines;
- delay andyield top-level suspending functions;
- Flow — cold asynchronous stream withflow builder and comprehensive operator set (filter,map, etc);
- Channel,Mutex, andSemaphore communication and synchronization primitives;
- coroutineScope,supervisorScope,withContext, andwithTimeout scope builders;
- MainScope() for Android and UI applications;
- SupervisorJob() andCoroutineExceptionHandler for supervision of coroutines hierarchies;
- select expression support and more.
- core/jvm — additional core features available on Kotlin/JVM:
- Dispatchers.IO dispatcher for blocking coroutines;
- Executor.asCoroutineDispatcher extension, custom thread pools, and more;
- Integrations with
CompletableFuture
and JVM-specific extensions.
- core/js — additional core features available on Kotlin/JS:
- Integration with
Promise
viaPromise.await andpromise builder; - Integration with
Window
viaWindow.asCoroutineDispatcher, etc.
- Integration with
- test — test utilities for coroutines:
- Dispatchers.setMain to overrideDispatchers.Main in tests;
- runTest andTestScope to test suspending functions and coroutines.
- debug — debug utilities for coroutines:
- DebugProbes API to probe, keep track of, print and dump active coroutines;
- CoroutinesTimeout test rule to automatically dump coroutines on test timeout.
- Automatic integration withBlockHound.
- reactive — modules that provide builders and iteration support for various reactive streams libraries:
- Reactive Streams (Publisher.collect,Publisher.awaitSingle,kotlinx.coroutines.reactive.publish, etc),
- Flow (JDK 9) (the same interface as for Reactive Streams),
- RxJava 2.x (rxFlowable,rxSingle, etc), and
- RxJava 3.x (rxFlowable,rxSingle, etc), and
- Project Reactor (flux,mono, etc).
- ui — modules that provide theMain dispatcher for various single-threaded UI libraries:
- Android, JavaFX, and Swing.
- integration — modules that provide integration with various asynchronous callback- and future-based libraries:
- GuavaListenableFuture.await, and Google Play ServicesTask.await;
- SLF4J MDC integration viaMDCContext.
- Presentations and videos:
- Kotlin Coroutines in Practice (Roman Elizarov at KotlinConf 2018,slides)
- Deep Dive into Coroutines (Roman Elizarov at KotlinConf 2017,slides)
- History of Structured Concurrency in Coroutines (Roman Elizarov at Hydra 2019,slides)
- Guides and manuals:
- Compatibility policy and experimental annotations
- Change log for kotlinx.coroutines
- Coroutines design document (KEEP)
- Full kotlinx.coroutines API reference
Add dependencies (you can also add other modules that you need):
<dependency> <groupId>org.jetbrains.kotlinx</groupId> <artifactId>kotlinx-coroutines-core</artifactId> <version>1.10.1</version></dependency>
And make sure that you use the latest Kotlin version:
<properties> <kotlin.version>2.0.0</kotlin.version></properties>
Add dependencies (you can also add other modules that you need):
dependencies { implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.1")}
And make sure that you use the latest Kotlin version:
plugins {// For build.gradle.kts (Kotlin DSL) kotlin("jvm") version"2.0.0"// For build.gradle (Groovy DSL) id"org.jetbrains.kotlin.jvm" version"2.0.0"}
Make sure that you havemavenCentral()
in the list of repositories:
repositories { mavenCentral()}
Addkotlinx-coroutines-android
module as a dependency when usingkotlinx.coroutines
on Android:
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.10.1")
This gives you access to the AndroidDispatchers.Maincoroutine dispatcher and also makes sure that in case of a crashed coroutine with an unhandled exception thatthis exception is logged before crashing the Android application, similarly to the way uncaught exceptions inthreads are handled by the Android runtime.
R8 and ProGuard rules are bundled into thekotlinx-coroutines-android
module.For more details see"Optimization" section for Android.
Thekotlinx-coroutines-core
artifact contains a resource file that is not required for the coroutines to operatenormally and is only used by the debugger. To exclude it at no loss of functionality, add the following snippet to theandroid
block in your Gradle file for the application subproject:
packagingOptions { resources.excludes+="DebugProbesKt.bin"}
Core modules ofkotlinx.coroutines
are also available forKotlin/JS andKotlin/Native.
In common code that should get compiled for different platforms, you can add a dependency tokotlinx-coroutines-core
right to thecommonMain
source set:
commonMain { dependencies { implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.1") }}
Platform-specific dependencies are recommended to be used only for non-multiplatform projects that are compiled only for target platform.
Kotlin/JS version ofkotlinx.coroutines
is published askotlinx-coroutines-core-js
(follow the link to get the dependency declaration snippet).
Kotlin/Native version ofkotlinx.coroutines
is published askotlinx-coroutines-core-$platform
where$platform
isthe target Kotlin/Native platform.Targets are provided in accordance withofficial K/N target support.
About
Library support for Kotlin coroutines