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

marcinmoskala

09/10/2019, 7:35 AM
Why
runBlocking
starts by default on a thread named "main" instead of using
Dispatchers.Default
?
Copy code
fun main() = runBlocking() {
    print(Thread.currentThread().name) // main
}

fun main() = runBlocking(Dispatchers.Default) {
    print(Thread.currentThread().name) // DefaultDispatcher-worker-1
}
j

Jacques Smuts

09/10/2019, 7:39 AM
It runs on the current thread you are on when you call it.
3
m

Marko Mitic

09/10/2019, 8:37 AM
As the name says, it doesn't launch, it runs
m

marcinmoskala

09/10/2019, 8:48 AM
@Marko Mitic This sounds convincing
k

kingsley

09/10/2019, 8:48 AM
On the other hand
suspend fun main() {}
actually does run on the default dispatcher
m

marcinmoskala

09/10/2019, 8:53 AM
This is the point, like AFAIK everything else
Copy code
suspend fun main() = coroutineScope<Unit> {
    launch {
        print(Thread.currentThread().name) // DefaultDispatcher-worker-1
    }
}

suspend fun main() {
    GlobalScope.launch {
        print(Thread.currentThread().name) // DefaultDispatcher-worker-1
    }.join()
}
3 Views