View kotlin_fundamentals_11.kt
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() |
View kotlin_fundamentals_10.kt
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) |
View kotlin_fundamentals_9.kt
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 | |
} |
View kotlin_fundamentals_8.kt
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 | |
} |
View kotlin_fundamentals_7.kt
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 |
View kotlin_fundamentals_6.kt
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 |
View kotlin_fundamentals_5.kt
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. |
View kotlin_fundamentals_4.kt
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 "" | |
} |
View kotlin_fundamentals_3.kt
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 |
View kotlin_fundamentals_2.kt
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