Skip to content

Instantly share code, notes, and snippets.

@droibit
Last active October 25, 2017 14:50
Show Gist options
  • Save droibit/4ec6da9be8742f7b3c3ee1acac29ffcf to your computer and use it in GitHub Desktop.
Save droibit/4ec6da9be8742f7b3c3ee1acac29ffcf to your computer and use it in GitHub Desktop.
Sample of combine coroutine context.
val parentJob = Job()
launch(parentJob + CommonPool) {
try {
Timber.d("[${Thread.currentThread().name}]: start job1.")
delay(1500L)
Timber.d("[${Thread.currentThread().name}]: stop job2.")
} catch (e: CancellationException) {
Timber.d("[${Thread.currentThread().name}]: cancel job1.")
}
}
launch(parentJob + CommonPool) {
try {
Timber.d("[${Thread.currentThread().name}]: start job2.")
delay(1050L)
Timber.d("[${Thread.currentThread().name}]: stop job2.")
} catch (e: CancellationException) {
Timber.d("[${Thread.currentThread().name}]: cancel job2.")
}
}
launch {
delay(1000)
parentJob.cancel()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment