liveBook · Manning

Chapter 15: Structured concurrency

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

This introduction explains why applications need to track coroutine lifetimes and presents structured concurrency as Kotlin’s built-in way to do so.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This introduction explains why applications need to track coroutine lifetimes and presents structured concurrency as Kotlin’s built-in way to do so.

Key terms: structured concurrency, hierarchy of coroutines, lifetimes, resource leaks, unnecessary work

15.1 Coroutine scopes establish structure between coroutines

Explains how coroutine scopes give launched coroutines parent-child relationships and let a parent wait for its children without tracking them manually.

Key terms: structured concurrency, coroutine scope, CoroutineScope, launch, async, parent-child relationship, runBlocking, child coroutines

15.1.1 Creating a coroutine scope: The coroutineScope function

This subsection introduces coroutineScope as a way to group child coroutines for concurrent work and wait for them to finish.

Key terms: coroutineScope, child coroutines, suspending function, concurrent decomposition of work, computeSum, suspending

15.1.2 Associating coroutine scopes with components: CoroutineScope

This section explains how a class can own a coroutine scope tied to its life cycle and distinguishes that use from suspending work decomposition.

Key terms: coroutineScope, suspending function, life cycle, start, stop, cancel, context, dispatcher, Job, SupervisorJob

15.1.3 The danger of GlobalScope

This section explains why launching coroutines in GlobalScope breaks structured concurrency and why application code should usually use a more appropriate scope.

Key terms: GlobalScope, structured concurrency, runBlocking, parent, children, life cycle, resource leaks, @DelicateCoroutinesApi, coroutineScope

15.1.4 Coroutine contexts and structured concurrency

Explains how coroutine contexts and Jobs follow the parent-child structure of coroutines, including how a child gets its dispatcher.

Key terms: coroutine context, Job, parent-child relationship, Dispatcher, Dispatchers.Default, parent dispatcher, job.parent, job.children, coroutineScope, coroutineContext.job

15.2 Cancellation

Defines cancellation and explains why stopping unneeded coroutine work matters for efficiency, resource use, and error handling.

Key terms: cancellation, unnecessary work, resource leaks, rogue coroutines, garbage collector, asynchronous network requests

15.2.1 Triggering cancellation

This subsection explains how to trigger cancellation using the handles returned by coroutine builders or the Job in a coroutine scope’s context.

Key terms: launch, async, Job, Deferred, cancel, coroutine context

15.2.2 Invoking cancellation automatically after a time limit has been exceeded

This subsection shows how to limit a coroutine computation’s execution time and how the two timeout functions report an expired limit.

Key terms: withTimeout, withTimeoutOrNull, timeout, cancellation, TimeoutCancellationException, try, catch

15.2.3 Cancellation cascades through all children

This subsection explains that cancelling a coroutine also cancels all its descendants, preventing child coroutines from continuing unnecessary work.

Key terms: structured concurrency, child coroutines, cancellation

15.2.4 Cancelled coroutines throw CancellationExceptions in special places

Explains where cancellation is delivered to a coroutine and why catching its exception can prevent cancellation.

Key terms: CancellationException, suspension points, suspending functions, withTimeoutOrNull, catch, UnsupportedOperationException

15.2.5 Cancellation is cooperative

Explains why a suspend function doing CPU-heavy work may ignore cancellation and how cancellable calls let it respond.

Key terms: cooperative, suspend modifier, suspension points, CancellationException, delay

15.2.6 Checking whether a coroutine has been cancelled

This subsection shows how to check whether a coroutine has been cancelled and how to respond to that check.

Key terms: CoroutineScope, isActive, ensureActive, CancellationException

15.2.7 Letting other coroutines play: The yield function

Explains how yield adds a cancellation point and gives other coroutines a chance to run on an occupied dispatcher.

Key terms: yield, suspension points, isActive, ensureActive, CancellationException, dispatcher

15.2.8 Keep cancellation in mind when acquiring resources

Explains why coroutine cancellation can leak resources and how to ensure they are closed.

Key terms: cancellation, CancellationException, finally block, AutoClosable, use

15.2.9 Frameworks can perform cancellation for you

Frameworks can provide and cancel coroutine scopes, so coroutines should be launched in a scope whose lifetime matches the work they need to do.

Key terms: coroutine scope, viewModelScope, ViewModel, cancellation, PipelineContext, CoroutineScope, request-level coroutine scope, Application, application coroutine scope

15.3 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 14: Coroutines Chapter 16: Flows →

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.