or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

channels.mdcoroutine-builders.mddispatchers.mdexception-handling.mdflow-api.mdindex.mdjob-management.mdjvm-integration.mdsynchronization.md
tile.json

tessl/maven-org-jetbrains-kotlinx--kotlinx-coroutines-core-jvm

JVM-specific implementation of kotlinx.coroutines core library providing coroutine primitives, builders, dispatchers, and synchronization primitives for asynchronous programming in Kotlin.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.jetbrains.kotlinx/kotlinx-coroutines-core-jvm@1.10.x

To install, run

npx @tessl/cli install tessl/maven-org-jetbrains-kotlinx--kotlinx-coroutines-core-jvm@1.10.0

index.mddocs/

kotlinx-coroutines-core-jvm

The JVM-specific implementation of kotlinx.coroutines library providing comprehensive support for asynchronous programming in Kotlin. This library offers coroutine primitives, builders, dispatchers, and synchronization mechanisms for concurrent programming with structured concurrency, cancellation support, and reactive streams through Flow API.

Package Information

  • Package Name: org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm
  • Package Type: maven
  • Language: Kotlin
  • Installation:
    dependencies {
        implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.10.2")
    }

Core Imports

import kotlinx.coroutines.*

For specific components:

import kotlinx.coroutines.flow.*
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.sync.*

Basic Usage

import kotlinx.coroutines.*

fun main() = runBlocking {
    // Launch a coroutine
    val job = launch {
        delay(1000L)
        println("Coroutine completed!")
    }
    
    // Create async computation
    val deferred = async {
        delay(500L)
        "Hello from async"
    }
    
    // Wait for results
    val result = deferred.await()
    println(result)
    job.join()
}

// Using different dispatchers
fun example() = runBlocking {
    withContext(Dispatchers.IO) {
        // Blocking I/O operation
        performBlockingIO()
    }
    
    withContext(Dispatchers.Default) {
        // CPU-intensive computation
        performComputation()
    }
}

Architecture

kotlinx-coroutines is built around several key architectural concepts:

  • Structured Concurrency: Coroutines are organized in hierarchical scopes ensuring proper cancellation and resource cleanup
  • Context Preservation: Coroutine context elements (dispatcher, job, exception handler) are preserved across suspension points
  • Cancellation Cooperation: Built-in cancellation mechanism that propagates through coroutine hierarchies
  • Dispatchers: Thread pool abstractions for different execution contexts (Default, IO, Main, Unconfined)
  • Suspending Functions: Non-blocking functions that can be paused and resumed without blocking threads
  • Cold Flows: Asynchronous sequences with backpressure and exception transparency
  • Hot Flows: Shared state and event streams (SharedFlow, StateFlow) for reactive programming

Capabilities

Coroutine Builders

Core functions for creating and launching coroutines with different execution patterns and lifecycle management.

fun CoroutineScope.launch(
    context: CoroutineContext = EmptyCoroutineContext,
    start: CoroutineStart = CoroutineStart.DEFAULT,
    block: suspend CoroutineScope.() -> Unit
): Job

fun <T> CoroutineScope.async(
    context: CoroutineContext = EmptyCoroutineContext,
    start: CoroutineStart = CoroutineStart.DEFAULT,
    block: suspend CoroutineScope.() -> T
): Deferred<T>

suspend fun <T> withContext(
    context: CoroutineContext,
    block: suspend CoroutineScope.() -> T
): T

fun <T> runBlocking(
    context: CoroutineContext = EmptyCoroutineContext,
    block: suspend CoroutineScope.() -> T
): T

Coroutine Builders

Dispatchers

Thread pool implementations for different execution contexts, with JVM-specific optimizations for IO operations and CPU-intensive tasks.

object Dispatchers {
    val Default: CoroutineDispatcher
    val Main: MainCoroutineDispatcher
    val IO: CoroutineDispatcher
    val Unconfined: CoroutineDispatcher
    
    @DelicateCoroutinesApi
    fun shutdown()
}

Dispatchers

Job Management

Lifecycle management for coroutines with hierarchical cancellation, completion tracking, and structured concurrency support.

interface Job : CoroutineContext.Element {
    val isActive: Boolean
    val isCompleted: Boolean
    val isCancelled: Boolean
    val children: Sequence<Job>
    
    fun start(): Boolean
    fun cancel(cause: CancellationException? = null)
    suspend fun join()
    val onJoin: SelectClause0
    fun invokeOnCompletion(handler: CompletionHandler): DisposableHandle
}

interface Deferred<out T> : Job {
    suspend fun await(): T
    val onAwait: SelectClause1<T>
    fun getCompleted(): T
    fun getCompletionExceptionOrNull(): Throwable?
}

Job Management

Flow API

Reactive streams implementation with cold flows for asynchronous sequences and hot flows for shared state management.

interface Flow<out T> {
    suspend fun collect(collector: FlowCollector<T>)
}

interface FlowCollector<in T> {
    suspend fun emit(value: T)
}

interface SharedFlow<out T> : Flow<T> {
    val replayCache: List<T>
    val subscriptionCount: StateFlow<Int>
}

interface StateFlow<out T> : SharedFlow<T> {
    val value: T
}

Flow API

Channels

Message passing primitives for communication between coroutines with various capacity and buffering strategies.

interface Channel<E> : SendChannel<E>, ReceiveChannel<E>

interface SendChannel<in E> {
    suspend fun send(element: E)
    fun trySend(element: E): ChannelResult<Unit>
    fun close(cause: Throwable? = null): Boolean
}

interface ReceiveChannel<out E> {
    suspend fun receive(): E
    fun tryReceive(): ChannelResult<E>
    suspend fun receiveCatching(): ChannelResult<E>
}

Channels

Synchronization Primitives

Thread-safe synchronization mechanisms designed for coroutines including mutexes, semaphores, and atomic operations.

interface Mutex {
    val isLocked: Boolean
    suspend fun lock(owner: Any? = null)
    fun tryLock(owner: Any? = null): Boolean
    fun unlock(owner: Any? = null)
}

interface Semaphore {
    val availablePermits: Int
    suspend fun acquire()
    fun tryAcquire(): Boolean
    fun release()
}

Synchronization

Exception Handling

Structured exception handling with coroutine-aware propagation, cancellation semantics, and supervisor patterns.

interface CoroutineExceptionHandler : CoroutineContext.Element {
    fun handleException(context: CoroutineContext, exception: Throwable)
}

fun SupervisorJob(parent: Job? = null): CompletableJob

suspend fun <T> supervisorScope(
    block: suspend CoroutineScope.() -> T
): T

Exception Handling

JVM Integration

Platform-specific features for JVM including executor integration, CompletableFuture interoperability, and thread-local context elements.

fun Executor.asCoroutineDispatcher(): ExecutorCoroutineDispatcher

fun <T> CompletableFuture<T>.asDeferred(): Deferred<T>
suspend fun <T> CompletableFuture<T>.await(): T

fun <T> ThreadLocal<T>.asContextElement(value: T): ThreadContextElement<T>

JVM Integration

Types

/** Select clause interfaces for coroutine select expressions */
sealed interface SelectClause0
sealed interface SelectClause1<out T>
sealed interface SelectClause2<in P, out Q>

/** Completion handler type alias */
typealias CompletionHandler = (cause: Throwable?) -> Unit