Created
January 30, 2021 18:16
-
-
Save GauravChaddha1996/538dabfd7f62b8cb59e06b8e0eca25b3 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 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() | |
// Join has to be used for example purposes otherwise the program | |
// will complete before 'T' completes. | |
// | |
// This is because since 'T' is a fire and forget task, we start it | |
// with launch coroutine builder which will return instantly with | |
// the representative job 'C' and the execution of this | |
// block will be complete. | |
// Also because since T is launched with a global scope, th | |
// So to wait for 'C' to complete we wait until it joins. | |
// Also note that since we start 'C' in GlobalScope | |
// it's not a child of the scope provided by the runBlocking. | |
// Hence the parent-child relationship doesn't apply. | |
parentCoroutineC.join() | |
} | |
println("Exiting main()") | |
exitProcess(0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment