Skip to content

Instantly share code, notes, and snippets.

@GauravChaddha1996
Created January 30, 2021 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GauravChaddha1996/6ea64545ea6b5b95668d94ff7e26ad2b to your computer and use it in GitHub Desktop.
Save GauravChaddha1996/6ea64545ea6b5b95668d94ff7e26ad2b to your computer and use it in GitHub Desktop.
runBlocking {
val scope = CoroutineScope(Dispatchers.IO)
val j = scope.launch {
println("Inside parent job")
delay(50)
val p = launch {
println("Inside child job")
// Notice how we now removed the isActive check
// to demonstrate that if a scope is cancelled,
// even an uncooperative coroutine is stopped
while (true) {
print(0)
}
println("\nEnding of child job")
}
p.join()
println("Ending of parent job")
}
delay(50)
scope.cancel()
}
Output:
Inside parent job
Inside child job
00000.....000000
// Notice how since the scope was cancelled,
// the "Ending of..." statements aren't printed.
// The execution was simply stopped.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment