Coroutines and Flow

Kotlin waits with coroutines, functions that pause without holding a thread, and streams results with Flow. Both come from kotlinx.coroutines 13,816 1.11.0 (May 2026); Android apps add org.jetbrains.kotlinx:kotlinx-coroutines-android:1.11.0 for Dispatchers.Main. The listings compile with Catalog.kt (Lambdas) and this helper, and run with java -Dkotlinx.coroutines.debug, which adds coroutine names to thread names. Timings come from this book's busy build machine; read the order.

Trace.kt: log() prints milliseconds since its first call, and the threadKotlin
private val t0 = System.nanoTime()
fun log(msg: String) = println("%4d ms [%s] %s"
  .format((System.nanoTime() - t0) / 1_000_000, Thread.currentThread().name, msg))

Subsections