【发布时间】:2019-11-17 16:25:38
【问题描述】:
我正在创建一个只有一个线程的线程池执行器,并在 Kotlin 程序中使用 Kotlin 的 asCoroutineDispatcher() 方法。当我从一个循环启动多个协程并记录线程名称时,我看到不同的名称 - pool1-thread1、pool3-thread1、pool9-thread-1 等。 为什么我在池中使用单线程时会有多个线程? Kotlin 管理线程池的方式是否不同?
// this is executed in loop
fun executeTask(url: String) {
GlobalScope.launch {
val result = runAsync(url)
Log.d("coroutineCheck", "$url\t\tStatus:$result")
}
}
//some blocking n/w IO goes in this method
//I log the thread name here
suspend fun runAsync(url: String): String = withContext(Executors.newFixedThreadPool(1).asCoroutineDispatcher()) {
}
【问题讨论】:
标签: java kotlin concurrency kotlin-coroutines