kotlinx.coroutines

repository·master·Indexed 11 days ago

https://github.com/kotlin/kotlinx.coroutines

A library providing support for Kotlin coroutines to enable efficient asynchronous programming through structured concurrency. It supports JVM, JS, Native, and Android platforms. The project includes specialized integration modules for Guava ListenableFuture, SLF4J MDC, and Google Play Services Tasks API, as well as test utilities via kotlinx-coroutines-test.

Tokens
68.5K
Snippets
188
Records
285
Agent score
96%

What's inside kotlinx.coroutines

  1. Overview of kotlinx.coroutines modules

    master

    The library is organized into several modules depending on your target platform and requirements:

    • core: Common coroutines for all platforms. Includes builders (launch, async), Dispatchers (Main, Default), Flow, Channel, Mutex, Semaphore, and scope builders (coroutineScope, supervisorScope, withContext, withTimeout).
    • core/jvm: JVM-specific features like Dispatchers.IO and CompletableFuture integration.
    • core/web & core/js: JS/Wasm support, including Promise integration.
    • test: Utilities for testing, including runTest, TestScope, and Dispatchers.setMain.
    • debug: Debugging tools like DebugProbes and CoroutinesTimeout.
    • reactive: Integration with Reactive Streams, RxJava 2/3, and Project Reactor.
    • ui: Dispatchers.Main implementations for Android, JavaFX, and Swing.
    • integration: Integration with Guava, Google Play Services, and SLF4J MDC.
  2. Overview of Coroutines for reactive streams

    master
    The reactive directory in kotlinx.coroutines provides specialized modules that allow seamless integration between Kotlin Coroutines and various reactive stream libraries. These modules provide utilities to convert between Coroutine types (like Flow) and reactive types (like Publisher or Observable).
  3. Integrate Google Play Services Tasks with Coroutines

    master

    The kotlinx-coroutines-play-services module provides extension functions to bridge Google Play Services Tasks API with Kotlin Coroutines. This allows you to treat asynchronous Play Services operations as suspending functions or Deferred values.

    Available Extension Functions

    NameDescription
    Task.asDeferred()Converts a Task into a Deferred
    Task.await()Awaits for completion of the Task (cancellable)
    Deferred.asTask()Converts a Deferred value to a Task
    import kotlinx.coroutines.tasks.await
    import kotlinx.coroutines.tasks.asDeferred
    import kotlinx.coroutines.tasks.asTask
  4. Use Dispatchers.JavaFx for JavaFX UI applications

    master

    The kotlinx-coroutines-javafx module provides the necessary coroutine dispatchers to interact with the JavaFX UI thread. It provides both a specific Dispatchers.JavaFx context and an implementation of Dispatchers.Main tailored for JavaFX applications. Use these dispatchers to ensure that UI updates and interactions with JavaFX components occur on the correct thread.

    For a detailed tutorial on UI programming with coroutines, refer to the Guide to UI programming with coroutines.

  5. Use Dispatchers.Swing for Swing UI applications

    master

    The kotlinx-coroutines-swing module provides the necessary coroutine dispatchers for interacting with Swing UI components. It provides:

    1. Dispatchers.Swing: A dispatcher specifically for the Swing event dispatch thread (EDT).
    2. Dispatchers.Main: An implementation of the main dispatcher that, when used in a Swing environment, defaults to the Swing EDT.

    This allows you to launch coroutines that can safely update UI components without manual SwingUtilities.invokeLater calls.

  6. Explore Coroutines integration modules

    master

    The kotlinx.coroutines project provides specialized integration modules to bridge coroutines with various asynchronous, callback-based, or future-based libraries. These modules allow you to convert external asynchronous types into coroutine-friendly constructs like Deferred or suspend functions.

    Available integration modules include:

    • Guava: Integration with Google Guava's ListenableFuture.
    • SLF4J: Integration with SLF4J's MDC (Mapped Diagnostic Context) to ensure coroutine context propagation in logging.
    • Play Services: Integration with Google Play Services' Tasks API (primarily for Android development).
  7. What is a Flow and how do flow pipelines work?

    master

    A Flow is a sequential stream of values produced asynchronously. While a suspending function returns a single value, a Flow allows you to work with multiple sequential values over time.

    You can build flow pipelines consisting of three roles:

    1. Emitter: Produces the values.
    2. Intermediate operators (optional): Consume values from an upstream flow, apply an operation, and return a new downstream flow.
    3. Collector: Consumes the final values.

    Values move through the pipeline from upstream (the emitter) to downstream (the collector).

    import kotlinx.coroutines.flow.*
    
    suspend fun main() {
        // The emitter produces values
        flowOf(0x4B, 0x6F, 0x74, 0x6C, 0x69, 0x6E)
            // The intermediate operator consumes values,
            // applies an operation, and returns another flow
            .map { value -> value.toChar() }
            // The collector consumes the transformed values
            .collect { updatedValue ->
                println("Say '$updatedValue'!")
            }
    }
  8. Migrate from BroadcastChannel to SharedFlow or StateFlow

    master
    In version 1.5.0, BroadcastChannel and ConflatedBroadcastChannel were marked as @ObsoleteCoroutinesApi. Users are encouraged to migrate to SharedFlow or StateFlow instead. Please refer to the specific documentation for these classes for the recommended migration path.