Hello, I was playing around with `coroutines` to u...
# getting-started
a
Hello, I was playing around with
coroutines
to understand them better on this playground. Although, the
runBlocking
works as I would expect it to work, with the structured concurrency and waiting for all of the coroutines to finish. But within the same code if you uncomment the
scope.launch
and comment out the
runBlocking
, The
main
completes without letting the scope finish. • Do the work in scope still completes? And if I want it to complete first, how would I do that, since I cannot call
join
on it.
j
Does the work in scope still completes?
No, the program terminates before everything is done, because you don't wait for it
And if I want it to complete first, how would I do that, since I cannot call join on it
If you want coroutines to complete, you have to wait for them. If you want your
main()
method to wait for them, it means you want to block the main thread, so
runBlocking
is appropriate. You could also choose to use
suspend fun main()
with or without a
coroutineScope { ... }
block.
👍 2
a
I can see now that both
IO
and
Default
executes on
DefaultDispatcher-worker-{NUM}
. Why does
Unconfined
says that it's working in
kotlinx.coroutines.DefaultExecutor
A coroutine dispatcher that is not confined to any specific thread.
And I am not sure if I understand the event loop terminology fully. Does it mean it can outlive it's scope to complete the work?
r
The next sentence in the documentation explains what that means:
It executes the initial continuation of a coroutine in the current call-frame and lets the coroutine resume in whatever thread that is used by the corresponding suspending function, without mandating any specific threading policy.