长期以来,我一直在高并发条件下使用Kotlin协同实验版本,性能一直很好。主要逻辑可以简化为以下代码:
// works fine in kotlin 1.2 with 3000+ QPS for a 40-core host
launch {
// running in ForkJoinPool.commonPool() by default
// non-blocking IO function
val result = supendFunction()
doSomething(result)
}但是,在我将kotlin更新为1.3之后,迁移到了coroutines的正式版本,如下所示
// kotlin 1.3 version
GlobalScope.launch {
// running in DefaultDispatcher
// non-blocking IO function
val result = supendFunction()
doSomething(result)
}CPU使用率从2%上升到50%,没有任何异常或抛出错误。我注意到的唯一不同之处是,协同线不再像以前那样在ForkJoinPool.commonPool()中执行。相反,它们在DefaultDispatcher线程中运行,比如DefaultDispatcher-worker-30。
我的问题是:
DefaultDispatcher要花费这么多的CPU?DefaultDispatcher代替ForkJoinPool.commonPool()?发布于 2018-11-13 11:12:22
DefaultDispatcher要花费这么多的CPU?它是一个完全不同的实现,它为多个性能目标优化,例如通过一个通道进行通信。它还有待于今后的改进。
DefaultDispatcher代替ForkJoinPool.commonPool()?实际上,它一直在使用Default dispatcher,但是Default的分辨率发生了变化。在实验阶段,它等同于CommonPool,但现在它更喜欢自定义实现。
将kotlinx.coroutines.scheduler系统属性设置为off。
https://stackoverflow.com/questions/53279409
复制相似问题