https://kotlinlang.org logo
#coroutines
Title
# coroutines
g

Gilles Barbier

03/11/2021, 10:21 AM
(I my real code, the runBlocking are processed from a method.invoke to "wait", that why I can' t use suspend functions there) - How can I avoid this unexpected behvior?
m

myanmarking

03/11/2021, 3:08 PM
what is the desired behavior? You want to execute launch block 1 and 2 sequentially ?
g

Gilles Barbier

03/11/2021, 3:10 PM
no, in parallel (of course here the implementation is simplified and do not have suspended functions, so it won't be the case)
m

myanmarking

03/11/2021, 3:11 PM
so you want parallel with interleaving results?
g

Gilles Barbier

03/11/2021, 3:11 PM
yes
m

myanmarking

03/11/2021, 3:12 PM
async{op1} async{op2} async1.await asyn2.await. Should do it?
you must call await only after declaring both async tho. otherwise it’s just sequential
are we talking about just 1 thread then ?
g

Gilles Barbier

03/11/2021, 3:15 PM
It's not possible in my implementation due to the complexity of the code. But my issue has been solved by using launch(Dispatcher.IO)
m

myanmarking

03/11/2021, 3:17 PM
ok. A note tho, you should not rely in the apparent order of execution. Launching two coroutines and expecting multiple interchangeable results with a specific order cannot be guaranteed
it’s better to rethink the design of the code
g

Gilles Barbier

03/11/2021, 3:17 PM
Thx. I understand that.
u

uli

03/11/2021, 6:52 PM
@myanmarking he is not relying on order. He is expecting the shorter delay to finish first
m

myanmarking

03/11/2021, 6:54 PM
ya, but you don’t have any guarantees. I am assuming the delay is some kind of computation
2 Views