1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.async import kotlinx.coroutines.awaitAll import kotlinx.coroutines.runBlocking
fun main() { val results = mutableListOf<String>() val list = listOf(1, 2, 3, 4, 5)
val deferred = list.map { GlobalScope.async { // 模拟采集 "data_$it" } }
runBlocking { val collected = deferred.awaitAll() results.addAll(collected) upload(results) }
}
private fun CoroutineScope.upload(results: MutableList<String>) { print("统一上传") }
|