64.5 Coroutine Dispatchers
Kotlin maintains threads for different types of asynchronous activity and, when launching a coroutine, it will be necessary to select the appropriate dispatcher from the following options:
•Dispatchers.Main – Runs the coroutine on the main thread and suitable for coroutines that need to make changes to the UI and as a general purpose option for performing lightweight tasks.
•Dispatchers.IO – Recommended for coroutines that perform network, disk or database operations.
•Dispatchers.Default – Intended for CPU intensive tasks such as sorting data or performing complex calculations.
The dispatcher is responsible for assigning coroutines to appropriate threads and suspending and resuming the coroutine during its lifecycle. In addition to the predefined dispatchers, it is also possible to create dispatchers for your own custom thread pools.