Sobes.tech
Junior — Middle

Kuidas toimub korutina täitmise peatamine programmeerimises?

sobes.tech AI

Vastus AI-lt

Suspending the execution of a coroutine in programming is done using the suspend keyword (for example, in Kotlin) or similar mechanisms in other languages. This allows the coroutine to temporarily yield control without blocking the thread, and resume later.

Example in Kotlin:

suspend fun fetchData() {
    val data = networkRequest() // suspension here, until the response arrives
    println(data)
}

In this example, networkRequest() is a suspend function that pauses the coroutine until the result is obtained, allowing other tasks to run in the meantime. The thread is not blocked, and the coroutine automatically resumes after the operation completes.