Skip to content

Instantly share code, notes, and snippets.

@GauravChaddha1996
Created January 30, 2021 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GauravChaddha1996/be4ccdc8c81d5931a02db9e15726840c to your computer and use it in GitHub Desktop.
Save GauravChaddha1996/be4ccdc8c81d5931a02db9e15726840c to your computer and use it in GitHub Desktop.
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