Sobes.tech
Junior — Middle

Mis on võtmesõna suspend eesmärk ja kasutus programmeerimiskeeles?

sobes.tech AI

Vastus AI-lt

The keyword suspend in the Kotlin language (often used in Android development) indicates a function that can be suspended and resumed later without blocking the thread.

This is the basis of coroutines — a mechanism for asynchronous and non-blocking programming. Functions marked with suspend can only be called from other suspend functions or coroutines.

Example:

suspend fun fetchData(): String {
    delay(1000) // suspends the coroutine for 1 second
    return "Data loaded"
}

Here, delay is also a suspend function that does not block the thread, but simply suspends the execution of the coroutine.