kotlinx.coroutines README

The project's README from github.com/Kotlin/kotlinx.coroutines. As one markdown file: README.md.

kotlinx.coroutines

Kotlin Stable JetBrains official project GitHub license Download Kotlin KDoc link Slack channel

Library support for Kotlin coroutines with multiplatform support. This is a companion version for the Kotlin 2.2.20 release.

suspend fun main() = coroutineScope {
    launch { 
       delay(1.seconds)
       println("Kotlin Coroutines World!") 
    }
    println("Hello")
}

Play with coroutines online here

Modules

Documentation

Using in your projects

Maven

Add dependencies (you can also add other modules that you need):

<dependency>
    <groupId>org.jetbrains.kotlinx</groupId>
    <artifactId>kotlinx-coroutines-core</artifactId>
    <version>1.11.0</version>
</dependency>

And make sure that you use the latest Kotlin version:

<properties>
    <kotlin.version>2.2.20</kotlin.version>
</properties>

Gradle

Add dependencies (you can also add other modules that you need):

dependencies {
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.11.0")
}

And make sure that you use the latest Kotlin version:

plugins {
    // For build.gradle.kts (Kotlin DSL)
    kotlin("jvm") version "2.2.20"
    
    // For build.gradle (Groovy DSL)
    id "org.jetbrains.kotlin.jvm" version "2.2.20"
}

Make sure that you have mavenCentral() in the list of repositories:

repositories {
    mavenCentral()
}

Android

Add kotlinx-coroutines-android module as a dependency when using kotlinx.coroutines on Android:

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.11.0")

This gives you access to the Android Dispatchers.Main coroutine dispatcher and also makes sure that in case of a crashed coroutine with an unhandled exception that this exception is logged before crashing the Android application, similarly to the way uncaught exceptions in threads are handled by the Android runtime.

R8 and ProGuard

R8 and ProGuard rules are bundled into the kotlinx-coroutines-android module. For more details see "Optimization" section for Android.

Avoiding including the debug infrastructure in the resulting APK

The kotlinx-coroutines-core artifact contains a resource file that is not required for the coroutines to operate normally and is only used by the debugger. To exclude it at no loss of functionality, add the following snippet to the android block in your Gradle file for the application subproject:

packagingOptions {
    resources.excludes += "DebugProbesKt.bin"
}

Multiplatform

Core modules of kotlinx.coroutines are also available for Kotlin/JS and Kotlin/Native.

In common code that should get compiled for different platforms, you can add a dependency to kotlinx-coroutines-core right to the commonMain source set:

commonMain {
    dependencies {
        implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.11.0")
    }
}

Platform-specific dependencies are recommended to be used only for non-multiplatform projects that are compiled only for target platform.

JS

Kotlin/JS version of kotlinx.coroutines is published as kotlinx-coroutines-core-js (follow the link to get the dependency declaration snippet).

Native

Kotlin/Native version of kotlinx.coroutines is published as kotlinx-coroutines-core-$platform where $platform is the target Kotlin/Native platform. Targets are provided in accordance with official K/N target support.

Building and Contributing

See Contributing Guidelines.

kotlinx.coroutines (Kotlin/kotlinx.coroutines)

Searching Database…

这是什么?kotlinx.coroutines 是 Kotlin 官方的库,用于通过协程编写异步、非阻塞的代码,支持多平台,包括 JVM、JS、Native 和 Wasm。
这是框架还是库?它是一个作为依赖添加的库,而不是框架。它提供 launch、async 等构建器,以及 Flow、通道和调度器,您可以在自己的代码中调用它们。
怎样把它添加到项目中?通过 Gradle 或 Maven 添加 kotlinx-coroutines-core 依赖,并确保使用的是最新版 Kotlin,且仓库中包含 mavenCentral();如果是多平台项目,请将它添加到 commonMain 源集中。
针对特定平台需要哪些模块?在 Android 上,如需使用 Main 调度器,请添加 kotlinx-coroutines-android;在 JS 和 Native 上,它分别以 kotlinx-coroutines-core-js 或按目标平台划分的 Native 构件发布;JavaFX、Swing 等 UI 工具包则有各自的 ui 模块。
在 Android 上配合 R8/ProGuard 使用时有哪些需要注意的地方?R8/ProGuard 规则已打包在 kotlinx-coroutines-android 中,但核心构件还附带一个仅用于调试的资源文件。如果您不想让它增大 APK 体积,可以通过 Gradle 代码片段将其从 APK 中移除。
它能与 RxJava 等响应式库集成吗?可以。reactive 模块为 Reactive Streams、RxJava 2.x/3.x、Project Reactor 以及 JDK 9 Flow 提供了构建器和迭代支持。
我能用这个库测试协程代码吗?可以。test 模块提供 runTest 和 TestScope,用于测试挂起函数;还提供 Dispatchers.setMain,用于在测试中替换 Main 调度器。
有没有用于调试协程的工具?有。debug 模块提供 DebugProbes API,用于跟踪、打印和导出活跃的协程;此外还包含 CoroutinesTimeout 测试规则以及与 BlockHound 的集成。
这个库中的 Flow 是什么?Flow 是 kotlinx.coroutines 中的冷异步流类型,通过 flow 构建器创建,并提供一整套操作符,如 filter 和 map。
Creator
Kotlin
Size
43 MB
Stars
13.8K
Age
10y
Commits · 90 Days