Skip to content

Instantly share code, notes, and snippets.

@mayojava
Last active October 26, 2018 10:42
Show Gist options
  • Select an option

  • Save mayojava/1906ff9789a3e4f5608f7a7843d3bbbd to your computer and use it in GitHub Desktop.

Select an option

Save mayojava/1906ff9789a3e4f5608f7a7843d3bbbd to your computer and use it in GitHub Desktop.
coroutine parent child relationship
fun main() = runBlocking<Unit>{
launch(Dispatchers.Default) {
repeat(5) {
println("coroutine 1: $it")
delay(500)
}
}
launch(Dispatchers.Default) {
repeat(5) {
println("coroutine 2: $it")
delay(500)
}
}
delay(1500)
coroutineContext[Job]?.cancel()
}
//console output
coroutine 1: 0
coroutine 2: 0
coroutine 1: 1
coroutine 2: 1
coroutine 1: 2
coroutine 2: 2
Exception in thread "main" kotlinx.coroutines.JobCancellationException: Job was cancelled; job=BlockingCoroutine{Cancelled}@61e717c2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment