Ahmed
01/23/2024, 5:54 PMcoroutines
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.Joffrey
01/23/2024, 5:58 PMDoes 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 itIf 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.Ahmed
01/23/2024, 6:17 PMIO
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?
Ruckus
01/24/2024, 5:43 PMIt 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.