Skip to content

Instantly share code, notes, and snippets.

@GauravChaddha1996
Created January 30, 2021 18:16
Show Gist options
  • Save GauravChaddha1996/538dabfd7f62b8cb59e06b8e0eca25b3 to your computer and use it in GitHub Desktop.
Save GauravChaddha1996/538dabfd7f62b8cb59e06b8e0eca25b3 to your computer and use it in GitHub Desktop.
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