Sobes.tech
Junior — Middle

Kas saaksite selgitada funktsiooni runBlocking eesmärki Kotlin Coroutines?

sobes.tech AI

Vastus AI-lt

The runBlocking function in Kotlin Coroutines is used to run coroutines in blocking mode, meaning it blocks the current thread until all coroutines inside it have completed.

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("Start")
}

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