JVM-specific implementation of kotlinx.coroutines core library providing coroutine primitives, builders, dispatchers, and synchronization primitives for asynchronous programming in Kotlin.
npx @tessl/cli install tessl/maven-org-jetbrains-kotlinx--kotlinx-coroutines-core-jvm@1.10.0The 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.
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.10.2")
}import kotlinx.coroutines.*For specific components:
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.sync.*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()
}
}kotlinx-coroutines is built around several key architectural concepts:
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
): TThread 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()
}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?
}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
}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>
}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()
}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
): TPlatform-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>/** 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