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 main() { | |
// Thread: Main | |
// Builder explanation: runBlocking is used for bridging coroutine | |
// code to normal code. | |
// It blocks the thread calling it until it's execution is complete. | |
// We call our taskT() on main thread and launch coroutines inside it. | |
// It returns the coroutine job as result. | |
runBlocking { | |
val parentCoroutineC = taskT() |
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) |
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 { | |
val resultT2 = t2(resultT1) | |
val resultT3 = t3(resultT2) | |
return@async resultT3 | |
} |
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 { | |
val resultT2 = t2(resultT1) | |
val resultT3 = t3(resultT2) | |
return@async resultT3 | |
} |
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 |
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") | |
/* | |
* Next we need to do task 'T1' which is a network call. So we call t1() | |
* | |
* suspension: Since t1() is marked suspend, it'll suspend | |
* the calling function. But since the code of 't1' isn't |
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
/** | |
* @return a job to represent the coroutine for 'T' | |
* */ | |
fun taskT(): Job { | |
/* | |
* Starting task 'T' by launching coroutine 'C' | |
* | |
* Task name: T | |
* Coroutine name: C | |
* Thread pool: IO is used since we do network calls etc. |
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
suspend fun t4(): String { | |
println("Inside t4() on ${Thread.currentThread().name}") | |
try { | |
// Emulate reading a file | |
delay(100) | |
return "resultT4" | |
} catch (e: Exception) { | |
// Ignore exception handling for now | |
return "" | |
} |
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
suspend fun t1(): Int { | |
println("Inside t1() on ${Thread.currentThread().name}") | |
// Emulate network call | |
delay(100) | |
return 1 | |
} | |
suspend fun t2(param1: Int): String { | |
println("Inside t2() on ${Thread.currentThread().name}") | |
// Emulate network call |
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 main() { | |
runBlocking { | |
val coroutineX = GlobalScope.launch { | |
val result1 = n1(5) | |
println("Hello I'll print after n1 is over") | |
println("Result1: $result1") | |
val result2 = n2(5) | |
println("Hello I'll print after n2 is over") | |
println("Result2: $result2") | |
} |
NewerOlder