Coroutines: The Async Builder
Coroutine Dispatchers
- Helps you control which thread or thread pool you want the coroutine to run on
- You can switch the thread coroutine when it suspends
Built in dispatchers:
Dispatchers.Default
- Shared thread pool
- Thread count usually matches the number of cores in the CPU
- Use for CPU tasks (like data transformations)
Dispatchers.IO
- Shared thread pool
- Thread count grows based on the number of coroutines using it
- Use for IO tasks (like network processing)
Dispatchers.Main
- Single main UI thread
- Used in Javascript, Swing, Android etc