In this question: Kotlin Coroutines choosing Dispatcher we can understand to use Dispatcher.Default
on CPU process, like an image/video conversion and Dispatcher.IO
when writing/reading files or API connection.
But in the class Dispatcher.kt
documentation, for the IO
you find this:
* This dispatcher shares threads with a [Default][Dispatchers.Default] dispatcher, so using * `withContext(Dispatchers.IO) { ... }` does not lead to an actual switching to another thread — * typically execution continues in the same thread.
So basically they run on the same thread anyway. There is a real difference or in the end it won't matter each one to use?
Thanks!
The default dispatcher is used when no other dispatcher is explicitly specified in the scope. It is represented by Dispatchers. Default and uses a shared background pool of threads. newSingleThreadContext creates a thread for the coroutine to run.
IO Dispatcher:It starts the coroutine in the IO thread, it is used to perform all the data operations such as networking, reading, or writing from the database, reading, or writing to the files eg: Fetching data from the database is an IO operation, which is done on the IO thread.
Dispatchers.IO is designed to be used when we block threads with I/O operations, for instance when we read/write files, use Android shared preferences, or call blocking functions. The code below takes around 1 second because Dispatchers.IO allows more than 50 active threads at the same time.
Dispatchers.IO - This dispatcher is optimized to perform disk or network I/O outside of the main thread. Examples include using the Room component, reading from or writing to files, and running any network operations.
The difference is that Dispatchers.Default
is limited to the number of CPU cores (with a minimum of 2) so only N (where N == cpu cores) tasks can run in parallel in this dispatcher.
On the IO dispatcher there are by default 64 threads, so there could be up to 64 parallel tasks running on that dispatcher.
The idea is that the IO dispatcher spends a lot of time waiting (IO blocked), while the Default dispatcher is intended for CPU intensive tasks, where there is little or no sleep.
So basically they run on the same thread anyway. There is a real difference or in the end it won't matter each one to use?
Your quote from the documentation just details an optimization Kotlin introduced: when switching contexts between Default
and IO
, you don't pay for the expensive handoff to another thread; instead the thread itself moves to the other pool.
This doesn't affect the overall properties of the thread pools behind the Default
and IO
dispatchers:
Default
has a fixed size hardcoded to the number of available processors, because that's what makes sense for CPU-intensive tasks.IO
is an elastic thread pool with a configurable max size. Threads in this pool are expected to spend most of their time in a non-runnable state, awaiting the completion of an IO operation, so it makes sense to have more of them than there are CPU cores.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With