https://kotlinlang.org logo
#getting-started
Title
# getting-started
t

thhh

05/02/2021, 4:35 PM
When using
thread
, the result is expected but when using
launch
from Kotlin coroutine, the result is wrong and not expected. Code: https://pl.kotl.in/cBD4j9XWb In the code, as expected when using the
thread
code in the for loop, the result prints 15000 but when using
launch
for loop, the result prints 0 !? which is not expected. Why is this happening and why
launch
is giving wrong answer?
e

ephemient

05/02/2021, 5:21 PM
you're blocking the executor with
Thread.sleep
.
delay
would give you the expected value.
but you should not be delaying and use
coroutineScope
or
join
instead.
also works
t

thhh

05/02/2021, 5:33 PM
Thank you for the extra details. It works.