Cette page est affichée en anglais. Une traduction française est en cours.
ConfigsMay 23, 2026·3 min de lecture

kotlinx.coroutines — Structured Concurrency Library for Kotlin

kotlinx.coroutines provides rich coroutine support for Kotlin including async/await, channels, flows, and structured concurrency primitives across JVM, JS, and Native targets.

Prêt pour agents

Cet actif peut être lu et installé directement par les agents

TokRepo expose une commande CLI universelle, un contrat d'installation, le metadata JSON, un plan selon l'adaptateur et le contenu raw pour aider les agents à juger l'adaptation, le risque et les prochaines actions.

Native · 98/100Policy : autoriser
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Established
Point d'entrée
kotlinx.coroutines Overview
Commande CLI universelle
npx tokrepo install 788a111c-5660-11f1-9bc6-00163e2b0d79

Introduction

kotlinx.coroutines is the official Kotlin library for asynchronous programming. It builds on Kotlin's language-level suspend keyword to provide structured concurrency, making concurrent code sequential-looking, cancellable, and leak-free by default.

What kotlinx.coroutines Does

  • Implements structured concurrency with coroutine scopes and automatic cancellation propagation
  • Provides Flow API for cold asynchronous data streams with backpressure
  • Offers Channels for hot inter-coroutine communication
  • Includes dispatchers for controlling thread pools (Default, IO, Main, Unconfined)
  • Supports multiplatform targets: JVM, JavaScript, and Kotlin/Native

Architecture Overview

The library layers on top of Kotlin's compiler-generated continuation-passing style. A CoroutineScope tracks all child coroutines via a Job hierarchy; if any child fails, structured concurrency cancels siblings and propagates the exception upward. Dispatchers map coroutines onto thread pools, while suspend functions yield execution without blocking threads.

Installation & Configuration

  • Add kotlinx-coroutines-core for JVM or multiplatform projects
  • Use kotlinx-coroutines-android for Android's Dispatchers.Main
  • Configure kotlinx-coroutines-test for deterministic testing with virtual time
  • Works with Gradle, Maven, and multiplatform Kotlin projects
  • Requires Kotlin 1.6+ (latest versions target Kotlin 2.x)

Key Features

  • async/await pattern for parallel decomposition of work
  • Flow operators (map, filter, combine, flatMapMerge) for reactive-style streams
  • SupervisorScope isolates failures so sibling coroutines survive
  • withTimeout and withTimeoutOrNull for deadline-based cancellation
  • Integration with Reactor, RxJava, JDK's CompletableFuture, and Guava ListenableFuture

Comparison with Similar Tools

  • RxJava/RxKotlin — observable-based reactive streams; coroutines offer simpler sequential syntax with fewer operators to learn
  • Project Loom (Virtual Threads) — JVM-level lightweight threads; coroutines add structured cancellation and Flow that Loom lacks
  • CompletableFuture — callback-heavy composition; coroutines read like synchronous code
  • Reactor (Project Reactor) — powerful but steep learning curve; coroutines integrate via kotlinx-coroutines-reactor
  • Arrow Fx — functional effect system for Kotlin; kotlinx.coroutines is the standard-library-blessed approach

FAQ

Q: How does structured concurrency prevent coroutine leaks? A: Every coroutine must be launched in a scope. When the scope completes or is cancelled, all child coroutines are cancelled automatically.

Q: Can I use coroutines with Spring Boot? A: Yes. Spring WebFlux supports suspend functions natively, and Spring MVC can use coroutines with kotlinx-coroutines-reactor.

Q: What is the difference between Flow and Channel? A: Flow is cold (emits on collection), while Channel is hot (emits regardless of collectors). Flow is preferred for most data stream patterns.

Q: Does kotlinx.coroutines work on Kotlin/Native? A: Yes. The library supports Kotlin/Native with a single-threaded dispatcher; multithreaded native support is under active development.

Sources

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires