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://search.maven.org/artifact/org.jetbrains.kotlinx/kotlinx-coroutines-core/1.6.4/pom) 7[](http://kotlinlang.org) 8[](https://kotlinlang.slack.com/messages/coroutines/) 9 10Library support for Kotlin coroutines with [multiplatform](#multiplatform) support. 11This is a companion version for the Kotlin `1.6.21` release. 12 13```kotlin 14suspend fun main() = coroutineScope { 15 launch { 16 delay(1000) 17 println("Kotlin Coroutines World!") 18 } 19 println("Hello") 20} 21``` 22 23> Play with coroutines online [here](https://pl.kotl.in/hG_tKbid_) 24 25## Modules 26 27* [core](kotlinx-coroutines-core/README.md) — common coroutines across all platforms: 28 * [launch] and [async] coroutine builders returning [Job] and [Deferred] light-weight futures with cancellation support; 29 * [Dispatchers] object with [Main][Dispatchers.Main] dispatcher for Android/Swing/JavaFx, and [Default][Dispatchers.Default] dispatcher for background coroutines; 30 * [delay] and [yield] top-level suspending functions; 31 * [Flow] — cold asynchronous stream with [flow][_flow] builder and comprehensive operator set ([filter], [map], etc); 32 * [Channel], [Mutex], and [Semaphore] communication and synchronization primitives; 33 * [coroutineScope][_coroutineScope], [supervisorScope][_supervisorScope], [withContext], and [withTimeout] scope builders; 34 * [MainScope()] for Android and UI applications; 35 * [SupervisorJob()] and [CoroutineExceptionHandler] for supervision of coroutines hierarchies; 36 * [select] expression support and more. 37* [core/jvm](kotlinx-coroutines-core/jvm/) — additional core features available on Kotlin/JVM: 38 * [Dispatchers.IO] dispatcher for blocking coroutines; 39 * [Executor.asCoroutineDispatcher][asCoroutineDispatcher] extension, custom thread pools, and more. 40* [core/js](kotlinx-coroutines-core/js/) — additional core features available on Kotlin/JS: 41 * Integration with `Promise` via [Promise.await] and [promise] builder; 42 * Integration with `Window` via [Window.asCoroutineDispatcher], etc. 43* [test](kotlinx-coroutines-test/README.md) — test utilities for coroutines: 44 * [Dispatchers.setMain] to override [Dispatchers.Main] in tests; 45 * [TestCoroutineScope] to test suspending functions and coroutines. 46* [debug](kotlinx-coroutines-debug/README.md) — debug utilities for coroutines: 47 * [DebugProbes] API to probe, keep track of, print and dump active coroutines; 48 * [CoroutinesTimeout] test rule to automatically dump coroutines on test timeout. 49 * Automatic integration with [BlockHound](https://github.com/reactor/BlockHound). 50* [reactive](reactive/README.md) — modules that provide builders and iteration support for various reactive streams libraries: 51 * Reactive Streams ([Publisher.collect], [Publisher.awaitSingle], [kotlinx.coroutines.reactive.publish], etc), 52 * Flow (JDK 9) (the same interface as for Reactive Streams), 53 * RxJava 2.x ([rxFlowable], [rxSingle], etc), and 54 * RxJava 3.x ([rxFlowable], [rxSingle], etc), and 55 * Project Reactor ([flux], [mono], etc). 56* [ui](ui/README.md) — modules that provide coroutine dispatchers for various single-threaded UI libraries: 57 * Android, JavaFX, and Swing. 58* [integration](integration/README.md) — modules that provide integration with various asynchronous callback- and future-based libraries: 59 * JDK8 [CompletionStage.await], Guava [ListenableFuture.await], and Google Play Services [Task.await]; 60 * SLF4J MDC integration via [MDCContext]. 61 62## Documentation 63 64* Presentations and videos: 65 * [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)) 66 * [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)) 67 * [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)) 68* Guides and manuals: 69 * [Guide to kotlinx.coroutines by example](https://kotlinlang.org/docs/coroutines-guide.html) (**read it first**) 70 * [Guide to UI programming with coroutines](ui/coroutines-guide-ui.md) 71 * [Debugging capabilities in kotlinx.coroutines](docs/topics/debugging.md) 72* [Compatibility policy and experimental annotations](docs/topics/compatibility.md) 73* [Change log for kotlinx.coroutines](CHANGES.md) 74* [Coroutines design document (KEEP)](https://github.com/Kotlin/KEEP/blob/master/proposals/coroutines.md) 75* [Full kotlinx.coroutines API reference](https://kotlinlang.org/api/kotlinx.coroutines/) 76 77## Using in your projects 78 79### Maven 80 81Add dependencies (you can also add other modules that you need): 82 83```xml 84<dependency> 85 <groupId>org.jetbrains.kotlinx</groupId> 86 <artifactId>kotlinx-coroutines-core</artifactId> 87 <version>1.6.4</version> 88</dependency> 89``` 90 91And make sure that you use the latest Kotlin version: 92 93```xml 94<properties> 95 <kotlin.version>1.6.21</kotlin.version> 96</properties> 97``` 98 99### Gradle 100 101Add dependencies (you can also add other modules that you need): 102 103```kotlin 104dependencies { 105 implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4") 106} 107``` 108 109And make sure that you use the latest Kotlin version: 110 111```kotlin 112plugins { 113 // For build.gradle.kts (Kotlin DSL) 114 kotlin("jvm") version "1.6.21" 115 116 // For build.gradle (Groovy DSL) 117 id "org.jetbrains.kotlin.jvm" version "1.6.21" 118} 119``` 120 121Make sure that you have `mavenCentral()` in the list of repositories: 122 123```kotlin 124repositories { 125 mavenCentral() 126} 127``` 128 129### Android 130 131Add [`kotlinx-coroutines-android`](ui/kotlinx-coroutines-android) 132module as a dependency when using `kotlinx.coroutines` on Android: 133 134```kotlin 135implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4") 136``` 137 138This gives you access to the Android [Dispatchers.Main] 139coroutine dispatcher and also makes sure that in case of a crashed coroutine with an unhandled exception that 140this exception is logged before crashing the Android application, similarly to the way uncaught exceptions in 141threads are handled by the Android runtime. 142 143#### R8 and ProGuard 144 145R8 and ProGuard rules are bundled into the [`kotlinx-coroutines-android`](ui/kotlinx-coroutines-android) module. 146For more details see ["Optimization" section for Android](ui/kotlinx-coroutines-android/README.md#optimization). 147 148#### Avoiding including the debug infrastructure in the resulting APK 149 150The `kotlinx-coroutines-core` artifact contains a resource file that is not required for the coroutines to operate 151normally and is only used by the debugger. To exclude it at no loss of functionality, add the following snippet to the 152`android` block in your Gradle file for the application subproject: 153 154```kotlin 155packagingOptions { 156 resources.excludes += "DebugProbesKt.bin" 157} 158``` 159 160### Multiplatform 161 162Core modules of `kotlinx.coroutines` are also available for 163[Kotlin/JS](https://kotlinlang.org/docs/reference/js-overview.html) and [Kotlin/Native](https://kotlinlang.org/docs/reference/native-overview.html). 164 165In common code that should get compiled for different platforms, you can add a dependency to `kotlinx-coroutines-core` right to the `commonMain` source set: 166 167```kotlin 168commonMain { 169 dependencies { 170 implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4") 171 } 172} 173``` 174 175No more additional dependencies are needed, platform-specific artifacts will be resolved automatically via Gradle metadata available since Gradle 5.3. 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://search.maven.org/artifact/org.jetbrains.kotlinx/kotlinx-coroutines-core-js/1.6.4/jar) 183(follow the link to get the dependency declaration snippet) and as [`kotlinx-coroutines-core`](https://www.npmjs.com/package/kotlinx-coroutines-core) NPM package. 184 185#### Native 186 187Kotlin/Native version of `kotlinx.coroutines` is published as 188[`kotlinx-coroutines-core-$platform`](https://mvnrepository.com/search?q=kotlinx-coroutines-core-) where `$platform` is 189the target Kotlin/Native platform. [List of currently supported targets](https://github.com/Kotlin/kotlinx.coroutines/blob/master/gradle/compile-native-multiplatform.gradle#L16). 190 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/-dispatchers/-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/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[TestCoroutineScope]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-test-coroutine-scope/index.html 245 246<!--- MODULE kotlinx-coroutines-debug --> 247<!--- INDEX kotlinx.coroutines.debug --> 248 249[DebugProbes]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-debug/kotlinx.coroutines.debug/-debug-probes/index.html 250 251<!--- INDEX kotlinx.coroutines.debug.junit4 --> 252 253[CoroutinesTimeout]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-debug/kotlinx.coroutines.debug.junit4/-coroutines-timeout/index.html 254 255<!--- MODULE kotlinx-coroutines-slf4j --> 256<!--- INDEX kotlinx.coroutines.slf4j --> 257 258[MDCContext]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-slf4j/kotlinx.coroutines.slf4j/-m-d-c-context/index.html 259 260<!--- MODULE kotlinx-coroutines-jdk8 --> 261<!--- INDEX kotlinx.coroutines.future --> 262 263[CompletionStage.await]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-jdk8/kotlinx.coroutines.future/await.html 264 265<!--- MODULE kotlinx-coroutines-guava --> 266<!--- INDEX kotlinx.coroutines.guava --> 267 268[ListenableFuture.await]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-guava/kotlinx.coroutines.guava/await.html 269 270<!--- MODULE kotlinx-coroutines-play-services --> 271<!--- INDEX kotlinx.coroutines.tasks --> 272 273[Task.await]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-play-services/kotlinx.coroutines.tasks/await.html 274 275<!--- MODULE kotlinx-coroutines-reactive --> 276<!--- INDEX kotlinx.coroutines.reactive --> 277 278[Publisher.collect]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-reactive/kotlinx.coroutines.reactive/collect.html 279[Publisher.awaitSingle]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-reactive/kotlinx.coroutines.reactive/await-single.html 280[kotlinx.coroutines.reactive.publish]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-reactive/kotlinx.coroutines.reactive/publish.html 281 282<!--- MODULE kotlinx-coroutines-rx2 --> 283<!--- INDEX kotlinx.coroutines.rx2 --> 284 285[rxFlowable]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-rx2/kotlinx.coroutines.rx2/rx-flowable.html 286[rxSingle]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-rx2/kotlinx.coroutines.rx2/rx-single.html 287 288<!--- MODULE kotlinx-coroutines-rx2 --> 289<!--- INDEX kotlinx.coroutines.rx2 --> 290<!--- MODULE kotlinx-coroutines-reactor --> 291<!--- INDEX kotlinx.coroutines.reactor --> 292 293[flux]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-reactor/kotlinx.coroutines.reactor/flux.html 294[mono]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-reactor/kotlinx.coroutines.reactor/mono.html 295 296<!--- END --> 297