Kotlin Tutorial

Coroutines

Master asynchronous programming in Kotlin with suspending functions, jobs, and scopes.

The Coroutine Paradigm

Coroutines are "lightweight threads." They allow you to write asynchronous code in a sequential style, avoiding "callback hell" and complex reactive chains.

Key Concepts

ConceptDescription
Suspending FunctionA function that can pause execution without blocking the thread.
CoroutineScopeDefines the lifecycle of the coroutine.
JobA handle to a coroutine that allows you to control its lifecycle.
DispatcherDetermines which thread or thread pool the coroutine runs on.

Basic Usage

kotlin
import kotlinx.coroutines.*

fun main() = runBlocking { // Blocks the main thread
    launch { // Launch a new coroutine in the scope of runBlocking
        delay(1000L) // Non-blocking delay for 1 second
        println("World!")
    }
    println("Hello,")
}

Structured Concurrency

Kotlin enforces structured concurrency, meaning new coroutines can only be launched in a specific CoroutineScope which delimits the lifetime of the coroutine.
kotlin
suspend fun doWork() = coroutineScope {
    launch { 
        // Child coroutine 1
    }
    launch {
        // Child coroutine 2
    }
    // scope waits for all children to complete
}

Dispatchers

  • Dispatchers.Main: UI operations (Android/JavaFX).
  • Dispatchers.IO: Network/Disk operations.
  • Dispatchers.Default: CPU-intensive tasks.

Summary Table

FeatureThreadCoroutine
MemoryExpensive (MBs)Cheap (Bytes)
SwitchingOS Context SwitchManaged by runtime
BlockingYesNo (Suspending)