liveBook · Manning

Dispatchers.Default

As explained in Kotlin in Action, 2E

Dispatchers.Default is a Kotlin coroutines dispatcher backed by a shared pool of worker threads for general-purpose coroutine execution.

Dispatchers.Default is the general-purpose choice for coroutine execution. It uses a thread pool sized to the available CPU cores, allowing work to run across multiple threads and in parallel on multicore machines. section 14.7.1

Select the default dispatcher explicitly

withContext(Dispatchers.Default) { // general-purpose or CPU-bound coroutine work } Use Dispatchers.Default when a coroutine needs no UI-thread confinement, blocking-API handling, or other specialized dispatcher. Coroutines suspend instead of blocking their worker threads, so the pool can support many suspended coroutines. section 14.7.1 section 14.9

Choose another dispatcher only when the work has a specific execution requirement:

  • Use Dispatchers.Main for operations that must run on a UI or main thread. section 14.7.1
  • Use Dispatchers.IO for APIs that inherently block threads. section 14.7.1
  • Use Dispatchers.Default as the starting point when no special constraint applies. section 14.7.1

How Kotlin in Action, 2E covers it

How deep it goes
Explains CPU-core-sized pooling, parallel execution, and suspension, but stops short of dispatcher implementation internals.
Examples
Uses a CPU-core-sized worker pool, multicore parallel execution, and a dispatcher-selection decision diagram.
What it assumes
Builds on coroutine builders, coroutine contexts, and suspending functions as the nonblocking execution model.
What you can do after
Apply it by launching coroutines or switching context for general-purpose work with Dispatchers.Default.
How it connects
Connects coroutines with coroutine builders, coroutine context, withContext, Dispatchers.Main, and Dispatchers.IO.

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.