liveBook · Manning

Chapter 14: Coroutines

What you will learn in chapter 14 of Kotlin in Action, 2E

The introduction explains why applications need asynchronous work and identifies coroutines as Kotlin’s approach to it.

After this chapter you will be able to

Section by section

Introduction

The introduction explains why applications need asynchronous work and identifies coroutines as Kotlin’s approach to it.

Key terms: asynchronously, concurrent tasks, blocking, coordinate and synchronize, coroutines, concurrent, parallel

14.1 Concurrency vs. parallelism

This section distinguishes concurrency from parallelism and explains how tasks can be concurrent even on a single CPU core.

Key terms: concurrency, parallelism, CPU core, Kotlin coroutines

14.2 Concurrency the Kotlin way: Suspending functions and coroutines

Introduces coroutines as a lightweight, non-blocking way to write concurrent Kotlin code and suspending functions as a way to make that code look sequential.

Key terms: coroutines, non-blocking, structured concurrency, suspending functions, blocking threads

14.3 Comparing threads and coroutines

This section compares the costs and limits of JVM threads with Kotlin coroutines, then explains how coroutines differ from and may complement Project Loom virtual threads.

Key terms: threads, system threads, blocked, coroutines, suspendable computations, JVM threads, structured concurrency, hierarchy, children, cancellation

14.4 Functions that can pause: Suspending functions

Introduces the sequential-looking style of Kotlin coroutine code and identifies suspending functions as functions that can pause.

Key terms: Kotlin coroutines, suspending functions, sequential

14.4.1 Code written with suspending functions looks sequential

Shows how suspending functions preserve sequential-looking application code while avoiding the costs of blocking a thread during network waits.

Key terms: blocking, thread, network request, suspend modifier, suspending functions, suspension, underlying libraries, Kotlin coroutines, login, loadUserData

14.5 Comparing coroutines to other approaches

The section compares callbacks, futures, and reactive streams with Kotlin coroutines for expressing successive concurrent operations.

Key terms: callbacks, callback hell, CompletableFuture, reactive streams, RxJava, Single, suspend modifier, coroutines, sequential look

14.5.1 Calling a suspending function

Explains where suspending functions can be called and how a program can make its first such call.

Key terms: suspending function, suspend modifier, non-suspending code, main function, coroutine builder functions

14.6 Entering the world of coroutines: Coroutine builders

This section defines a coroutine, introduces the purposes of three coroutine builders, and explains where most coroutine APIs come from.

Key terms: coroutine, suspendable computation, suspending function, coroutine builder functions, runBlocking, launch, async, Kotlin standard library, kotlinx.coroutines

14.6.1 From regular code into the realm of coroutines: The runBlocking function

This section shows how runBlocking lets regular code call suspending functions and explains why its blocking behavior can still be useful.

Key terms: runBlocking, coroutine builder, suspending functions, child coroutines, launch, suspended

14.6.2 Creating start-and-forget coroutines: The launch function

This section shows how launch starts child coroutines for work without a return value and how suspension lets them take turns on one thread.

Key terms: launch, child coroutine, start-and-forget, Job, runBlocking, delay, suspension point, interleaved execution, suspending functions, state

14.6.3 Awaitable computations: The async builder

Explains how async starts concurrent computations whose results can be awaited, when to use it instead of ordinary suspending calls, and where async/await functionality lives in Kotlin.

Key terms: async, Deferred<T>, await, concurrently, suspending function calls, independent tasks, kotlinx.coroutines, suspending functions

14.7 Deciding where your code should run: Dispatchers

This section explains how dispatchers determine the threads a coroutine uses and how thread pools reuse threads to run tasks.

Key terms: dispatcher, coroutine, thread, thread pool, threads, tasks

14.7.1 Choosing a dispatcher

This section explains how to choose among Kotlin’s built-in coroutine dispatchers based on ordinary work, UI-thread requirements, and blocking APIs.

Key terms: dispatcher, parent coroutine, Dispatchers.Default, thread pool, suspends, Dispatchers.Main, UI thread, main thread, Dispatchers.IO, blocking API

14.7.2 Passing a dispatcher to a coroutine builder

This section shows how to run an entire coroutine on a chosen dispatcher by passing it to a coroutine builder.

Key terms: dispatcher, coroutine builder, launch, default dispatcher

14.7.3 Using withContext to switch the dispatcher within a coroutine

This section shows how an existing coroutine can switch dispatchers to perform a UI update after background work.

Key terms: withContext, dispatcher, Dispatchers.Main, Dispatchers.IO, UI framework

14.7.4 Coroutines and dispatchers aren’t a magical fix for thread-safety concerns

This section explains why a single coroutine avoids internal parallelism but shared data modified by parallel coroutines still needs thread-safety measures.

Key terms: multithreaded dispatcher, coroutine, thread-safety, synchronization, Mutex, critical section, AtomicInteger, ConcurrentHashMap, single-threaded dispatcher, withContext

14.8 Coroutines carry additional information in their coroutine context

Explains what a coroutine context contains, how to inspect it, and how to supply context elements when starting or switching coroutines.

Key terms: CoroutineContext, CoroutineDispatcher, Job, CoroutineName, CoroutineExceptionHandler, suspending function, compiler intrinsic, + operator, coroutine builder, withContext

14.9 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 13: DSL construction Chapter 15: Structured concurrency →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.