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/3fb1d108bf1ed84d9abdc1135b4ffd01 to your computer and use it in GitHub Desktop.
Save GauravChaddha1996/3fb1d108bf1ed84d9abdc1135b4ffd01 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 check that the job's isActive
while (true && isActive) {
print(0)
}
println("\nEnding of child job")
}
// Cancelling the child job after a 50ms delay
delay(1)
p.cancelAndJoin()
println("Ending of parent job")
}
j.join()
}
Output:
Inside parent job
Inside child job
000000....000000
Ending of child job
Ending of parent job
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment