运行并行操作并获取结果-async {...}是正确的方法吗?

时间:2020-01-28 16:07:04

标签: android kotlin-coroutines

Android Studio 3.6

我需要下一个:

  1. 字符串列表(文件名)
  2. 每个方法doSomeLongOperation(fileName)必须以 parallel 独立地执行。例如。方法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的协程,这是正确的方法吗?

0 个答案:

没有答案