Yes, but this is according to what I said, isn’t it? As long as runBlocking()
is running, the main dispatcher can’t schedule anything. If we try to use launch(Dispatchers.Main)
, withContext(Dispatchers.Main)
etc. it won’t execute. Dispatchers.Default
works fine, but delay()
internally falls back to Dispatchers.Main
, so it is also blocked.
Your question is probably about using launch()
without any dispatcher (your “first” example) - it works normally, even despite the fact it also uses the main thread. This is because the main thread isn’t really blocked. It is blocked from the main dispatcher perspective. runBlocking()
“hijacks” the thread that invoked it and runs an event loop on it. As long as it has something to do, it occupies this thread entirely, so from the external perspective the thread is blocked. But internally the thread runs coroutines started inside runBlocking()
. So in your “first” example coroutines are also scheduled on the JavaFX main thread, but not using the main dispatcher. They are scheduled using runBlocking
internal dispatcher, which is not blocked and which in this specific case also uses the main thread.