liveBook · Manning

Concurrency

As explained in Kotlin in Action, 2E

Concurrency is the execution of multiple pieces of computation during overlapping periods of time, whether or not they execute physically at the same instant.

Concurrency means working on multiple computations during overlapping periods. It keeps user interfaces responsive, lets applications handle several network requests, and allows servers to continue serving other requests while one runs. section 1.2.4 section 14.0

suspend fun processUser(credentials: Credentials) {
    val user = authenticate(credentials)                       #1
    val data = loadUserData(user)                              #2
    val profilePicture = loadImage(data.imageID)               #3
    // ...
}
 
suspend fun authenticate(c: Credentials): User { /* ... */ }   #4
suspend fun loadUserData(u: User): Data { /* ... */ }
suspend fun loadImage(id: Int): Image { /* ... */ }

A suspending processUser function reads sequentially while each network call suspends without blocking its underlying thread. section 1.2.4

Concurrency does not require physical simultaneity: a single CPU core can interleave tasks. Parallelism executes tasks physically at the same time on multiple cores; Kotlin coroutines support both. section 14.1

Loading images concurrently

async starts loading two images independently, await waits for both results, and combineImages produces the final image inside a coroutineScope. section 1.2.4

Kotlin structures concurrent work with:

  • Suspending functions, which pause execution and resume later without changing sequential-looking code. section 14.9
  • Coroutine builders such as runBlocking, launch, and async, which create coroutine computations. section 14.9
  • Dispatchers and withContext, which select or switch the thread or thread pool used for execution. section 14.9

How Kotlin in Action, 2E covers it

How deep it goes
Defines concurrency versus parallelism and sketches coroutine mechanics, while deferring detailed suspension, async, and structured-concurrency treatment.
Examples
Uses responsive interfaces, concurrent network calls, sequential user loading, and concurrent image loading with async and await.
What you can do after
Apply it via Kotlin coroutines to suspend network work, run independent operations concurrently, and coordinate results without blocking threads.
How it connects
Connects concurrency with coroutines, parallelism, suspending functions, threads, callbacks, futures, reactive streams, dispatchers, and structured concurrency.

Also explained in

Compare how 23 books explain concurrency →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kotlin in Action, 2E. The section links point to the parts of the book it draws on.