android - 运行并行操作并获得结果 - async{...} 是正确的方式吗?
问题描述
安卓工作室 3.6
我需要下一个:
- 字符串列表(文件名)
- 每个方法都
doSomeLongOperation(fileName)
必须并行且独立地执行。例如,方法doSomeLongOperation(fileName1)
绝不能等待方法的响应doSomeLongOperation(fileName2)
。 - 每个结果都
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 协程的正确方法吗?
解决方案
推荐阅读
- reactjs - 如何在 React 中重命名对象数据响应
- python - Spyder 未启动
- greasemonkey - 如何在开发过程中重新加载 ViolentMonkey 用户脚本?
- linux - 如何消除短袜中 0.1 秒的间隙?
- python - 类型错误:fit_transform() 缺少 1 个必需的位置参数:'X'
- php - 如何将我的 PC 转换为在线服务器?
- r - 确定一个数据帧的日期时间是否在另一个数据帧之后的一小时内
- javascript - 复制文件的 Gulpfile 任务应优化图像(如果有)
- c# - .Net Core 托管服务需要 HttpContext
- r - R中x轴的描述