Suppose I have an arbitrary list of `suspend` func...
# coroutines
v
Suppose I have an arbitrary list of
suspend
functions p(), q(),..., and I want them to run in parallel/concurrently. Is this a valid approach?
g
yes, you can use launch (if you don’t need result of invocation) This covered in official guide https://github.com/Kotlin/kotlinx.coroutines/blob/master/docs/composing-suspending-functions.md#concurrent-using-async
v
Thanks. This is all very new to me 🙂
u
Why do you need the outer launch?
v
Turns out I don't, it just appeared in the tutorial I was working from and I copied blindly 🙂. I've taken it out.
d
I don't think the code you wrote works. The
launch
invocations are using dispatcher provided by
runBlocking
scope, which uses the
Which queues the coroutines on the same thread
Instead, you can consider using GlobalScope, or even better, declaring your function as an extension on CoroutineScope interface. You can also put it in a component that has its own CoroutineScope.
Or just pass a dispatcher as context to the top most launch
u
@Dico why do you think the code would not work on a single thread?
If all coroutines are surrended must of the time a single thread will do just fine.
d
What I meant to say is that I think the code will run on a single thread (as I eloquently explained) while the poster explicitly wants to run tasks in parallel.
@uli