kotlinx.coroutines icon indicating copy to clipboard operation
kotlinx.coroutines copied to clipboard

Coroutines guide should highlight the dangers of `runBlocking`

Open dkhalanskyjb opened this issue 7 months ago • 2 comments
trafficstars

Reported by @joffrey-bion in the Kotlinlang Slack.

Currently, the coroutines guide uses runBlocking freely to explain the concept of coroutines. However, in practice, runBlocking does not have such a prominent role and is often error-prone: https://github.com/Kotlin/kotlinx.coroutines/issues/4242

At the top level, there is an alternative to runBlocking:

suspend fun main() {
    coroutineScope {
        // code goes here
    }
}

It's not without its issues, though: it silently introduces multithreading (because no dispatcher is specified, Dispatchers.Default is used).

I don't see a way around using the single-threaded runBlocking for some of the examples, but the issues with runBlocking should be highlighted.

dkhalanskyjb avatar Apr 15 '25 11:04 dkhalanskyjb