Difference between usage of Dispatcher IO and Default

前端 未结 2 1384
孤街浪徒
孤街浪徒 2021-02-04 02:44

In this question: Kotlin Coroutines choosing Dispatcher we can understand to use Dispatcher.Default on CPU process, like an image/video conversion and Dispatc

2条回答
  •  醉话见心
    2021-02-04 03:11

    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.

提交回复
热议问题