Sobes.tech
Junior — Middle

Can you explain the purpose of the runBlocking function in Kotlin Coroutines?

sobes.tech AI

Answer from AI

The runBlocking function in Kotlin Coroutines is used to start coroutines in a blocking manner, meaning it blocks the current thread until all coroutines inside it complete.

This is useful, for example, at program entry points (main function) or in tests, where you need to run asynchronous code and wait for the result synchronously.

Example of usage:

fun main() = runBlocking {
    launch {
        delay(1000L)
        println("Hello from coroutine")
    }
    println("Starting")
}

Here, runBlocking blocks the main thread until the coroutine inside finishes.

Can you explain the purpose of the runBlocking… - sobes.tech