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

David Glasser

11/07/2019, 7:59 PM
Hmm. So I was under the impression that the "normal" way to make a program that uses coroutine was
fun main() = runBlocking {
We just discovered that this leads to coroutines being run single-threaded by default, not in Dispatchers.Default. This is a big surprise! Is the normal pattern supposed to be
fun main() = runBlocking(Dispatchers.Default) {
?
or are we just supposed to be using
suspend fun main()
now?
o

octylFractal

11/07/2019, 8:00 PM
yes, by default
runBlocking
is an event loop that runs on the thread that called it.
suspend fun main()
is just shorthand for using
runBlocking
iirc
d

David Glasser

11/07/2019, 8:01 PM
so do most people use
fun main = runBlocking(Dispatchers.Default)
?
o

octylFractal

11/07/2019, 8:01 PM
personally I typically don't have coroutines at the top level, so I end up with my own application
CoroutineScope
that I `launch`/etc. off of
d

David Glasser

11/07/2019, 8:02 PM
yeah this is for server
m

marstran

11/07/2019, 8:13 PM
I think you can do this:
Copy code
suspend fun main() = coroutineScope { ... }
r

Ran Magen

11/08/2019, 10:04 PM
that throws
Copy code
Error: Main method not found in class Temp, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
o

octylFractal

11/08/2019, 10:25 PM
is
main()
inside a
class Temp { }
declaration? if not,
TempKt
is the correct class name
r

Ran Magen

11/15/2019, 10:21 PM
I'm pressing the little play button in Intellij, so it's choosing the parameters.. Are you saying
Copy code
suspend fun main(args: Array<String>) = coroutineScope { .. }
should work as a main function?
o

octylFractal

11/15/2019, 10:34 PM
yes. that's correct
6 Views