首页 > 解决方案 > 运行并行操作并获得结果 - async{...} 是正确的方式吗?

问题描述

安卓工作室 3.6

我需要下一个:

  1. 字符串列表(文件名)
  2. 每个方法都doSomeLongOperation(fileName)必须并行独立地执行。例如,方法doSomeLongOperation(fileName1) 绝不能等待方法的响应doSomeLongOperation(fileName2)
  3. 每个结果都doSomeLongOperation(fileName)不同,必须添加到列表中

所以假设 的持续时间doSomeLongOperation(fileName)是 5 秒。

所以如果我从 12:00:00 开始,那么一切都必须在 12:00:05 完成

所以这里的片段:

 suspend fun testParallel(filesList: List<String>) =
            withContext(Dispatchers.IO) {
                Debug.d(TAG, "testParallel: start, filesList = $filesList")
                val resultList = mutableListOf<Deferred<Int>>()
                filesList.forEach({
                    val result = async { doSomeLongOperation(it) }
                    resultList.add(result)
                })
                Debug.d(TAG, "testParallel: resultList = ${resultList.awaitAll()}")
            }

        suspend fun doSomeLongOperation(fileName: String): Int {
            //val duration = 1000L * Random().nextInt(20)
            val duration = 1000L * 5
            Debug.d(TAG, "doSomeLongOperation: START, fileName = $fileName, duration = $duration")
            delay(duration)
            val random = Random().nextInt(100)
            Debug.d(
                TAG,
                "doSomeLongOperation: FINISH, fileName = $fileName, duration = $duration, random = $random"
            )
            return random
        }

在这里输出:

01-28 17:57:19.662  testParallel: start, filesList = [file_1, file_2, file_3, file_4, file_5,
01-28 17:57:19.670  doSomeLongOperation: START, fileName = file_1, duration = 5000           
01-28 17:57:19.671  doSomeLongOperation: START, fileName = file_2, duration = 5000           
01-28 17:57:19.678  doSomeLongOperation: START, fileName = file_4, duration = 5000           
01-28 17:57:19.684  doSomeLongOperation: START, fileName = file_3, duration = 5000           
01-28 17:57:19.684  doSomeLongOperation: START, fileName = file_5, duration = 5000           
01-28 17:57:19.685  doSomeLongOperation: START, fileName = file_7, duration = 5000           
01-28 17:57:19.685  doSomeLongOperation: START, fileName = file_6, duration = 5000           
01-28 17:57:19.685  doSomeLongOperation: START, fileName = file_8, duration = 5000           
01-28 17:57:19.685  doSomeLongOperation: START, fileName = file_9, duration = 5000           
01-28 17:57:19.685  doSomeLongOperation: START, fileName = file_10, duration = 5000

...

01-28 17:57:24.674  doSomeLongOperation: FINISH, fileName = file_1, duration = 5000, random = 0
01-28 17:57:24.675  doSomeLongOperation: FINISH, fileName = file_2, duration = 5000, random = 17
01-28 17:57:24.678  doSomeLongOperation: FINISH, fileName = file_4, duration = 5000, random = 16
01-28 17:57:24.684  doSomeLongOperation: FINISH, fileName = file_3, duration = 5000, random = 75
01-28 17:57:24.684  doSomeLongOperation: FINISH, fileName = file_5, duration = 5000, random = 19
01-28 17:57:24.685  doSomeLongOperation: FINISH, fileName = file_7, duration = 5000, random = 59
01-28 17:57:24.685  doSomeLongOperation: FINISH, fileName = file_6, duration = 5000, random = 2
01-28 17:57:24.686  doSomeLongOperation: FINISH, fileName = file_8, duration = 5000, random = 38
01-28 17:57:24.686  doSomeLongOperation: FINISH, fileName = file_9, duration = 5000, random = 64
01-28 17:57:24.686  doSomeLongOperation: FINISH, fileName = file_10, duration = 5000, random = 94

01-28 17:57:24.686  testParallel: resultList = [0, 17, 75, 16, 19, 2, 59, 38, 64, 94]

如您所见,流程开始01-28 17:57:19所有方法都在 5 秒后完成

01-28 17:57:24

并且每个结果都doSomeLongOperation被添加到restulList. 好的。它工作正常。为此,我使用 Kotlin 的方法:async

问题是:

这是 Kotlin 协程的正确方法吗?

标签: androidkotlin-coroutines

解决方案


推荐阅读