Created
January 30, 2021 18:16
-
-
Save GauravChaddha1996/be4ccdc8c81d5931a02db9e15726840c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun taskT(): Job { | |
return GlobalScope.launch(Dispatchers.IO) { | |
println("Starting C on ${Thread.currentThread().name}\n") | |
val resultT1 = t1() | |
println("Starting task T2,T3 on ${Thread.currentThread().name}") | |
val jobT2T3 = async { | |
println("Inside Task T2,T3 on ${Thread.currentThread().name}") | |
// t2 and t3 are suspend functions and will suspend this block | |
// until it returns the result | |
val resultT2 = t2(resultT1) | |
val resultT3 = t3(resultT2) | |
return@async resultT3 | |
} | |
println("Starting task T4,T5 on ${Thread.currentThread().name}\n") | |
val jobT4T5 = async(Dispatchers.Default, start = CoroutineStart.LAZY) { | |
println("Inside Task T4,T5 on ${Thread.currentThread().name}") | |
val resultT4 = t4() | |
val resultT5 = t5(scope = this, param4 = resultT4) | |
return@async resultT5 | |
} | |
delay(100) | |
println("Going to await result of task T2,T3 and T4,T5") | |
val combinedResult = jobT2T3.await() + "_" + jobT4T5.await() | |
println("\nThe final result is $combinedResult") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment