小编dar*_*ker的帖子

为什么 GlobalScope.async 似乎比 coroutineScope.async 性能更高

我目前正在开发小型应用程序来学习 Kotlin(使用一些 Spring Boot 功能来加快工作速度)。不幸的是,我无法理解异步运行的协程返回一些结果,这很可能是由于对协程作用域的理解不好。

以下代码是我的“主要”方法

@Component
class GameRunner() : CommandLineRunner {

    override fun run(vararg args: String) = runBlocking {

        val players = listOf(Player(1),Player(2),Player(3),Player(4))
        val results = mutableListOf<Results>()
        val measureTimeMillis = measureTimeMillis {
            val deferredResults = mutableListOf<Deferred<List<Results>>>()
            for (i in 0 until 250) {
                deferredResults.add(async { executeLadderRound(players.shuffled()) })
            }
            results.addAll(deferredResults.awaitAll().flatten())
        }
        // ... present the results ...
    }
    
    // for the record, this method plays a simulation of a board game that is fully blocking CPU intensive process, there are …
Run Code Online (Sandbox Code Playgroud)

kotlin kotlin-coroutines

0
推荐指数
1
解决办法
150
查看次数

标签 统计

kotlin ×1

kotlin-coroutines ×1