我正在尝试使用Kotlin协程,以下是我的代码:
fun main(args: Array<String>) = runBlocking {
val cores = Runtime.getRuntime().availableProcessors()
println("number of cores: $cores")
val jobs = List(10) {
async(CommonPool) {
delay(100)
println("async #$it on thread ${Thread.currentThread().name}")
}
}
jobs.forEach { it.join() }
}
这是我的输出:
number of cores: 4
async number:0 on thread ForkJoinPool.commonPool-worker-2
async number:2 on thread ForkJoinPool.commonPool-worker-3
async number:3 on thread ForkJoinPool.commonPool-worker-3
async number:4 on thread ForkJoinPool.commonPool-worker-3
async number:5 on thread ForkJoinPool.commonPool-worker-3
async number:1 on thread ForkJoinPool.commonPool-worker-1
async number:7 on thread ForkJoinPool.commonPool-worker-3
async number:6 on thread ForkJoinPool.commonPool-worker-2
async number:9 on thread ForkJoinPool.commonPool-worker-3
async number:8 on thread ForkJoinPool.commonPool-worker-1
根据Roman Elizarov在另一个有关协程的问题的答案,启动只是创建新协程,而CommonPool将协程分派到ForkJoinPool.commonPool(),后者确实使用多个线程,在本例中执行多个CPU。
根据Java 8 文档:
“对于需要单独或自定义池的应用程序,可以使用给定的目标并行性级别构建ForkJoinPool;默认情况下,等于可用处理器的数量。”
为什么只使用了3个工作线程?即使我增加异步任务的数量到1000+,这些工作线程仍然是相同的3个。
我的配置: Mac / High Sierra带有双核CPU(具有超线程,因此有4个可见核心),Kotlin 1.2,kotlinx-coroutines-core:0.19.3和JVM 1.8。