【发布时间】:2020-03-21 05:26:02
【问题描述】:
在这个问题中:Kotlin Coroutines choosing Dispatcher
我们可以理解在 CPU 进程上使用Dispatcher.Default,如图像/视频转换和在写入/读取文件或 API 连接时使用Dispatcher.IO。
但是在Dispatcher.kt 类文档中,对于IO,你会发现:
* 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.
所以基本上它们无论如何都在同一个线程上运行。有真正的区别还是最终每个使用都无关紧要?
谢谢!
【问题讨论】:
-
该评论表明
Dispatchers.IO使用了一组线程,这些线程是Dispatchers.Default使用的线程的超集。