Shouldn't join resume after onCompletion?
See original GitHub issueHello,
please consider the following code:
fun foo() {
Thread.sleep(1000)
println("alright this have been executed")
}
fun main(args: Array<String>) {
val job = Job()
job.cancel()
GlobalScope.launch(job) {
try {
println("do stuff")
} finally {
foo()
}
}
runBlocking {
println("before join")
job.cancelAndJoin()
println("afterJoin")
}
}
foo()
is not invoked at all, because the job is cancelled before being scheduled.
So what are my options, if I want to make sure foo()
is always executed?
One may try to use onCompletion
like this:
fun foo() {
Thread.sleep(1000)
println("alright this have been executed")
}
fun main(args: Array<String>) {
val job = Job()
job.cancel()
GlobalScope.launch(job, onCompletion = { foo() }) { println("do stuff") }
runBlocking {
println("before join")
job.cancelAndJoin()
println("afterJoin")
}
}
But it doesn’t help, because join
doesn’t seem to care if onCompletion
have been executed or not.
And here comes my question: Shouldn’t join
resume only after the execution onCompletion
?
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Stop Wasting Time on Your Resume: A Lesson From The ...
While phenomenal resume tips (and debates) abound, there's one point everyone seems to miss: Out of all the job search activities, ...
Read more >invokeOnCompletion - Kotlin
Registers handler that is synchronously invoked once on completion of this job. ... There is no need to dispose the handler after completion...
Read more >Waiting until the task finishes - swift - Stack Overflow
It will wait until the loop finishes but in this case your main thread will block. You can also do the same thing...
Read more >MediaPlayer - Android Developers
A MediaPlayer object must first enter the Prepared state before playback can be started. ... MediaPlayer is resuming playback after filling buffers.
Read more >15 Things You Should Not Include in a Resume - TopResume
What you decide to not include in a resume is just as important as what you choose to include. Here's a list to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ops. We need to promote
CoroutineStart.ATOMIC
from internal to experimental.For now,
start = ATOMIC
withtry / finally { ... }
is the only guaranteed way to make sure that something is done before other coroutines that.join
resume. In the future we might add back support or something similar toonCompletion
, but it has to be composable (don’t want to hack every coroutine builder again).Btw, if you are only interested in exceptions, you can install
CorouinteExceptionHandler
. It is guaranteed to be invoked before all joining coroutines can resume.