When launching a coroutine, you may wish to control which thread or thread pool the coroutine actually executes on. A coroutine dispatcher allows you to do this. When a coroutine begins execution, or is resumed after suspension, the dispatcher is used to select a thread for that execution.
The thread selected at the start of a coroutine need not be the same thread selected after resumption. For example, a thread pool may be used and any of the available threads could be selected after a function resumes.
The coroutine library ships with several built in dispatchers. These are as follows:
- Dispatchers.Default: This uses a shared thread pool, generally set to the number of cores in the CPU (although the minimum is two). This kind of dispatcher is recommended for CPU bound tasks.
- Dispatchers.IO: This uses a shared thread pool that will grow as required, depending...