Last active
October 26, 2018 10:42
-
-
Save mayojava/1906ff9789a3e4f5608f7a7843d3bbbd to your computer and use it in GitHub Desktop.
coroutine parent child relationship
This file contains hidden or 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() = 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