Created
January 30, 2021 18:15
-
-
Save GauravChaddha1996/7bd8fcaea5d8cdccd33ccda2ff6ae08c 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() | |
/* | |
* Next we need to do task 'T2' whose output is input to task 'T3' | |
* both of which are a network call. | |
* | |
* Builder explanation: We need the result from these two task for our | |
* final result. So we'll use async builder. It'll return a deferred | |
* object which we store in variable 'jobT2T3'. | |
* Deferred is a job whose value one can await. | |
* Task name: T2, T3 | |
* Coroutine name: C2, C3 respectively | |
* Thread pool: Since nothing is mentioned, while mixing context the parent | |
* dispatcher is used i.e. IO. | |
* suspension: async is started as soon as it's called but it won't | |
* suspend the calling function. | |
* Async or launch will also not suspend the calling coroutine | |
* since the former returns a deferred and the latter is fire | |
* and forget type. | |
* scope: As a child if nothing is mentioned it inherits the scope of | |
* the parent. So this job will also run in global scope. | |
* */ | |
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 | |
} | |
// omitted code here | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment