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

oday

09/04/2022, 10:39 PM
I dont know why I’m not able to run this coroutines code in standalone IntelliJ, I have a clue as to the Looper not being there means something is trying to access something Android specific, but not much clue otherwise.. is it because of the
Main
Dispatchers called there? there’s no Main thread like UI dispatcher in kotlin itself https://hastebin.com/eyolacixis.properties
yea, using IO works, or using Dispatchers.setMain(Dispatchers.IO) works as well but im not sure what i want here
yea there is no Main dispatcher here that does what i do, the code doesnt even go inside the launch block
just gets fired off immediately and program exists, i need it to run on the main thread here
j

Joffrey

09/05/2022, 5:59 AM
You wouldn't want
GlobalScope.launch
anyway there, because the
main()
method would then complete and your program would exit before the coroutine even runs at all. If you want to run a coroutine from
main()
on the main thread and wait for it, use
runBlocking
- it's really meant for this.
o

oday

09/05/2022, 8:47 AM
yes I dont know why i missed on all of that logically, it’s just im following this book and this is literally the very first example, and Im following it and convinced that this is how it should be since it “runs” fine in the book, let me show you
if you use runBlocking, then yes it will hit the code inside that coroutine and execute in the same way it does on the website I think they’re lying
This is, in effect, "hello, world" in the realm of coroutines. If you run this, you will see one message printed immediately and the other after a short delay.
lol
j

Joffrey

09/05/2022, 9:32 AM
Oh my. Yeah I don't have much context here, but for starters,
Dispatchers.Main
doesn't have a default implementation without extra modules. You either need android, or javafx, or swing - something that brings some kind of UI thread that is used in the Main dispatcher. So they must be cheating and provide one of those for it to work on the website. Secondly,
GlobalScope
is generally discouraged unless you need a coroutine that runs for the whole life of the application, or if you want to manage the lifecycle of the coroutine manually (and know what you're doing). So it should definitely not be in a hello world example for beginners.
o

oday

09/05/2022, 9:41 AM
im going to try and run this on Android just to see if it at least runs like they say
ok so I didn’t try to run that on Android to see what would happen I now just want to know how i can replicate this behavior that is in that sample i linked where
This executes immediately
is fired …immediately and then the coroutine inside the runBlocking runs
all of what i tried will first go into the coroutine block and then at the end runs the print statement for
executes immediately
well you can simply say
Copy code
fun main() {
    GlobalScope.launch {
        stallForTime()
        println("This is executed after the delay")
    }

    println("This is executed immediately")

    Thread.sleep(3000)
}
j

Joffrey

09/05/2022, 9:27 PM
That sample runs on Kotlin/JS, not Kotlin/JVM. I just noticed now, I'm not sure if you had noticed before. In that case, there probably is a
Main
dispatcher defined (tied to the JS event loop), and maybe this executes all enqueued tasks before terminating the program (that wouldn't surprise me)
17 Views