Skip to content

Instantly share code, notes, and snippets.

@Axrorxoja
Created December 27, 2020 16:53
Show Gist options
  • Save Axrorxoja/d44b226259dbb491fa49d48275333ac6 to your computer and use it in GitHub Desktop.
Save Axrorxoja/d44b226259dbb491fa49d48275333ac6 to your computer and use it in GitHub Desktop.
Coroutine parallel launch
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
fun main() {
runBlocking {
launch {
val resultDeferred1 = async { fun1() }
val resultDeferred2 = async { fun2() }
val resultDeferred3 = async { fun3() }
val resultDeferred4 = async { fun4() }
println("result1:${resultDeferred1.await()}\n" +
"result2:${resultDeferred2.await()}\n" +
"result3:${resultDeferred3.await()}\n" +
"result4:${resultDeferred4.await()}")
}
}
}
suspend fun fun1(): Any {
delay(100)
return "fun1 finished"
}
suspend fun fun2(): Any {
delay(200)
return "fun2 finished"
}
suspend fun fun3(): Any {
delay(300)
return "fun3 finished"
}
suspend fun fun4(): Any {
delay(400)
return "fun3 finished"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment