Hmm. So I was under the impression that the "norma...
# coroutines
d
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
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
so do most people use
fun main = runBlocking(Dispatchers.Default)
?
o
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
yeah this is for server
m
I think you can do this:
Copy code
suspend fun main() = coroutineScope { ... }
r
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
is
main()
inside a
class Temp { }
declaration? if not,
TempKt
is the correct class name
r
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
yes. that's correct