1# kotlinx.coroutines 2 3[](https://kotlinlang.org/docs/components-stability.html) 4[](https://confluence.jetbrains.com/display/ALL/JetBrains+on+GitHub) 5[](https://www.apache.org/licenses/LICENSE-2.0) 6[](https://central.sonatype.com/artifact/org.jetbrains.kotlinx/kotlinx-coroutines-core/1.10.1) 7[](http://kotlinlang.org) 8[](https://kotlinlang.org/api/kotlinx.coroutines/) 9[](https://kotlinlang.slack.com/messages/coroutines/) 10 11Library support for Kotlin coroutines with [multiplatform](#multiplatform) support. 12This is a companion version for the Kotlin `2.0.0` release. 13 14```kotlin 15suspend fun main() = coroutineScope { 16 launch { 17 delay(1000) 18 println("Kotlin Coroutines World!") 19 } 20 println("Hello") 21} 22``` 23 24> Play with coroutines online [here](https://pl.kotl.in/9zva88r7S) 25 26## Modules 27 28* [core](kotlinx-coroutines-core/README.md) — common coroutines across all platforms: 29 * [launch] and [async] coroutine builders returning [Job] and [Deferred] light-weight futures with cancellation support; 30 * [Dispatchers] object with [Main][Dispatchers.Main] dispatcher for Android/Swing/JavaFx (which require the corresponding artifacts in runtime) and Darwin (included out of the box), and [Default][Dispatchers.Default] dispatcher for background coroutines; 31 * [delay] and [yield] top-level suspending functions; 32 * [Flow] — cold asynchronous stream with [flow][_flow] builder and comprehensive operator set ([filter], [map], etc); 33 * [Channel], [Mutex], and [Semaphore] communication and synchronization primitives; 34 * [coroutineScope][_coroutineScope], [supervisorScope][_supervisorScope], [withContext], and [withTimeout] scope builders; 35 * [MainScope()] for Android and UI applications; 36 * [SupervisorJob()] and [CoroutineExceptionHandler] for supervision of coroutines hierarchies; 37 * [select] expression support and more. 38* [core/jvm](kotlinx-coroutines-core/jvm/) — additional core features available on Kotlin/JVM: 39 * [Dispatchers.IO] dispatcher for blocking coroutines; 40 * [Executor.asCoroutineDispatcher][asCoroutineDispatcher] extension, custom thread pools, and more; 41 * Integrations with `CompletableFuture` and JVM-specific extensions. 42* [core/js](kotlinx-coroutines-core/js/) — additional core features available on Kotlin/JS: 43 * Integration with `Promise` via [Promise.await] and [promise] builder; 44 * Integration with `Window` via [Window.asCoroutineDispatcher], etc. 45* [test](kotlinx-coroutines-test/README.md) — test utilities for coroutines: 46 * [Dispatchers.setMain] to override [Dispatchers.Main] in tests; 47 * [runTest] and [TestScope] to test suspending functions and coroutines. 48* [debug](kotlinx-coroutines-debug/README.md) — debug utilities for coroutines: 49 * [DebugProbes] API to probe, keep track of, print and dump active coroutines; 50 * [CoroutinesTimeout] test rule to automatically dump coroutines on test timeout. 51 * Automatic integration with [BlockHound](https://github.com/reactor/BlockHound). 52* [reactive](reactive/README.md) — modules that provide builders and iteration support for various reactive streams libraries: 53 * Reactive Streams ([Publisher.collect], [Publisher.awaitSingle], [kotlinx.coroutines.reactive.publish], etc), 54 * Flow (JDK 9) (the same interface as for Reactive Streams), 55 * RxJava 2.x ([rxFlowable], [rxSingle], etc), and 56 * RxJava 3.x ([rxFlowable], [rxSingle], etc), and 57 * Project Reactor ([flux], [mono], etc). 58* [ui](ui/README.md) — modules that provide the [Main][Dispatchers.Main] dispatcher for various single-threaded UI libraries: 59 * Android, JavaFX, and Swing. 60* [integration](integration/README.md) — modules that provide integration with various asynchronous callback- and future-based libraries: 61 * Guava [ListenableFuture.await], and Google Play Services [Task.await]; 62 * SLF4J MDC integration via [MDCContext]. 63 64## Documentation 65 66* Presentations and videos: 67 * [Kotlin Coroutines in Practice](https://www.youtube.com/watch?v=a3agLJQ6vt8) (Roman Elizarov at KotlinConf 2018, [slides](https://www.slideshare.net/elizarov/kotlin-coroutines-in-practice-kotlinconf-2018)) 68 * [Deep Dive into Coroutines](https://www.youtube.com/watch?v=YrrUCSi72E8) (Roman Elizarov at KotlinConf 2017, [slides](https://www.slideshare.net/elizarov/deep-dive-into-coroutines-on-jvm-kotlinconf-2017)) 69 * [History of Structured Concurrency in Coroutines](https://www.youtube.com/watch?v=Mj5P47F6nJg) (Roman Elizarov at Hydra 2019, [slides](https://speakerdeck.com/elizarov/structured-concurrency)) 70* Guides and manuals: 71 * [Guide to kotlinx.coroutines by example](https://kotlinlang.org/docs/coroutines-guide.html) (**read it first**) 72 * [Guide to UI programming with coroutines](ui/coroutines-guide-ui.md) 73 * [Debugging capabilities in kotlinx.coroutines](docs/topics/debugging.md) 74* [Compatibility policy and experimental annotations](docs/topics/compatibility.md) 75* [Change log for kotlinx.coroutines](CHANGES.md) 76* [Coroutines design document (KEEP)](https://github.com/Kotlin/KEEP/blob/master/proposals/coroutines.md) 77* [Full kotlinx.coroutines API reference](https://kotlinlang.org/api/kotlinx.coroutines/) 78 79## Using in your projects 80 81### Maven 82 83Add dependencies (you can also add other modules that you need): 84 85```xml 86<dependency> 87 <groupId>org.jetbrains.kotlinx</groupId> 88 <artifactId>kotlinx-coroutines-core</artifactId> 89 <version>1.10.1</version> 90</dependency> 91``` 92 93And make sure that you use the latest Kotlin version: 94 95```xml 96<properties> 97 <kotlin.version>2.0.0</kotlin.version> 98</properties> 99``` 100 101### Gradle 102 103Add dependencies (you can also add other modules that you need): 104 105```kotlin 106dependencies { 107 implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.1") 108} 109``` 110 111And make sure that you use the latest Kotlin version: 112 113```kotlin 114plugins { 115 // For build.gradle.kts (Kotlin DSL) 116 kotlin("jvm") version "2.0.0" 117 118 // For build.gradle (Groovy DSL) 119 id "org.jetbrains.kotlin.jvm" version "2.0.0" 120} 121``` 122 123Make sure that you have `mavenCentral()` in the list of repositories: 124 125```kotlin 126repositories { 127 mavenCentral() 128} 129``` 130 131### Android 132 133Add [`kotlinx-coroutines-android`](ui/kotlinx-coroutines-android) 134module as a dependency when using `kotlinx.coroutines` on Android: 135 136```kotlin 137implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.10.1") 138``` 139 140This gives you access to the Android [Dispatchers.Main] 141coroutine dispatcher and also makes sure that in case of a crashed coroutine with an unhandled exception that 142this exception is logged before crashing the Android application, similarly to the way uncaught exceptions in 143threads are handled by the Android runtime. 144 145#### R8 and ProGuard 146 147R8 and ProGuard rules are bundled into the [`kotlinx-coroutines-android`](ui/kotlinx-coroutines-android) module. 148For more details see ["Optimization" section for Android](ui/kotlinx-coroutines-android/README.md#optimization). 149 150#### Avoiding including the debug infrastructure in the resulting APK 151 152The `kotlinx-coroutines-core` artifact contains a resource file that is not required for the coroutines to operate 153normally and is only used by the debugger. To exclude it at no loss of functionality, add the following snippet to the 154`android` block in your Gradle file for the application subproject: 155 156```kotlin 157packagingOptions { 158 resources.excludes += "DebugProbesKt.bin" 159} 160``` 161 162### Multiplatform 163 164Core modules of `kotlinx.coroutines` are also available for 165[Kotlin/JS](https://kotlinlang.org/docs/reference/js-overview.html) and [Kotlin/Native](https://kotlinlang.org/docs/reference/native-overview.html). 166 167In common code that should get compiled for different platforms, you can add a dependency to `kotlinx-coroutines-core` right to the `commonMain` source set: 168 169```kotlin 170commonMain { 171 dependencies { 172 implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.1") 173 } 174} 175``` 176 177Platform-specific dependencies are recommended to be used only for non-multiplatform projects that are compiled only for target platform. 178 179#### JS 180 181Kotlin/JS version of `kotlinx.coroutines` is published as 182[`kotlinx-coroutines-core-js`](https://central.sonatype.com/artifact/org.jetbrains.kotlinx/kotlinx-coroutines-core-js/1.10.1) 183(follow the link to get the dependency declaration snippet). 184 185#### Native 186 187Kotlin/Native version of `kotlinx.coroutines` is published as 188[`kotlinx-coroutines-core-$platform`](https://central.sonatype.com/search?q=kotlinx-coroutines-core&namespace=org.jetbrains.kotlinx) where `$platform` is 189the target Kotlin/Native platform. 190Targets are provided in accordance with [official K/N target support](https://kotlinlang.org/docs/native-target-support.html). 191## Building and Contributing 192 193See [Contributing Guidelines](CONTRIBUTING.md). 194 195<!--- MODULE kotlinx-coroutines-core --> 196<!--- INDEX kotlinx.coroutines --> 197 198[launch]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/launch.html 199[async]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/async.html 200[Job]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/index.html 201[Deferred]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-deferred/index.html 202[Dispatchers]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/index.html 203[Dispatchers.Main]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/-main.html 204[Dispatchers.Default]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/-default.html 205[delay]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/delay.html 206[yield]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/yield.html 207[_coroutineScope]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/coroutine-scope.html 208[_supervisorScope]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/supervisor-scope.html 209[withContext]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/with-context.html 210[withTimeout]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/with-timeout.html 211[MainScope()]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-main-scope.html 212[SupervisorJob()]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-supervisor-job.html 213[CoroutineExceptionHandler]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-exception-handler/index.html 214[Dispatchers.IO]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-i-o.html 215[asCoroutineDispatcher]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/as-coroutine-dispatcher.html 216[Promise.await]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/await.html 217[promise]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/[js]promise.html 218[Window.asCoroutineDispatcher]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/as-coroutine-dispatcher.html 219 220<!--- INDEX kotlinx.coroutines.flow --> 221 222[Flow]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html 223[_flow]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/flow.html 224[filter]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/filter.html 225[map]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/map.html 226 227<!--- INDEX kotlinx.coroutines.channels --> 228 229[Channel]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.channels/-channel/index.html 230 231<!--- INDEX kotlinx.coroutines.selects --> 232 233[select]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.selects/select.html 234 235<!--- INDEX kotlinx.coroutines.sync --> 236 237[Mutex]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.sync/-mutex/index.html 238[Semaphore]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.sync/-semaphore/index.html 239 240<!--- MODULE kotlinx-coroutines-test --> 241<!--- INDEX kotlinx.coroutines.test --> 242 243[Dispatchers.setMain]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/set-main.html 244[runTest]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/run-test.html 245[TestScope]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-test-scope/index.html 246 247<!--- MODULE kotlinx-coroutines-debug --> 248<!--- INDEX kotlinx.coroutines.debug --> 249 250[DebugProbes]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-debug/kotlinx.coroutines.debug/-debug-probes/index.html 251 252<!--- INDEX kotlinx.coroutines.debug.junit4 --> 253 254[CoroutinesTimeout]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-debug/kotlinx.coroutines.debug.junit4/-coroutines-timeout/index.html 255 256<!--- MODULE kotlinx-coroutines-slf4j --> 257<!--- INDEX kotlinx.coroutines.slf4j --> 258 259[MDCContext]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-slf4j/kotlinx.coroutines.slf4j/-m-d-c-context/index.html 260 261<!--- MODULE kotlinx-coroutines-jdk8 --> 262<!--- INDEX kotlinx.coroutines.future --> 263<!--- MODULE kotlinx-coroutines-guava --> 264<!--- INDEX kotlinx.coroutines.guava --> 265 266[ListenableFuture.await]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-guava/kotlinx.coroutines.guava/await.html 267 268<!--- MODULE kotlinx-coroutines-play-services --> 269<!--- INDEX kotlinx.coroutines.tasks --> 270 271[Task.await]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-play-services/kotlinx.coroutines.tasks/await.html 272 273<!--- MODULE kotlinx-coroutines-reactive --> 274<!--- INDEX kotlinx.coroutines.reactive --> 275 276[Publisher.collect]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-reactive/kotlinx.coroutines.reactive/collect.html 277[Publisher.awaitSingle]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-reactive/kotlinx.coroutines.reactive/await-single.html 278[kotlinx.coroutines.reactive.publish]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-reactive/kotlinx.coroutines.reactive/publish.html 279 280<!--- MODULE kotlinx-coroutines-rx2 --> 281<!--- INDEX kotlinx.coroutines.rx2 --> 282 283[rxFlowable]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-rx2/kotlinx.coroutines.rx2/rx-flowable.html 284[rxSingle]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-rx2/kotlinx.coroutines.rx2/rx-single.html 285 286<!--- MODULE kotlinx-coroutines-rx2 --> 287<!--- INDEX kotlinx.coroutines.rx2 --> 288<!--- MODULE kotlinx-coroutines-reactor --> 289<!--- INDEX kotlinx.coroutines.reactor --> 290 291[flux]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-reactor/kotlinx.coroutines.reactor/flux.html 292[mono]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-reactor/kotlinx.coroutines.reactor/mono.html 293 294<!--- END --> 295