【发布时间】:2021-01-01 16:06:25
【问题描述】:
是否可以为当前线程创建Dispatcher?检查此示例代码作为我想要完成的示例:
val dispatcher = if (parallel) {
Dispatcher.Default
} else {
// What should I write here so I just use the current thread to run doStuff?
}
val deferredList = list.map {
async(dispatcher) { doStuff(it) }
}
【问题讨论】:
-
您可能确实不能。您可以使用当前的 Dispatcher,但这与当前线程不同。
-
runBlocking是使用当前线程作为其内部调度程序的线程。
标签: multithreading kotlin kotlin-coroutines