Junior — Middle
How can the execution of coroutines be delayed in an asynchronous program?
sobes.tech AI
Answer from AI
In Kotlin, to delay the execution of coroutines, you can use the delay() function, which suspends the current coroutine without blocking the thread.
Example:
import kotlinx.coroutines.*
fun main() = runBlocking {
println("Start")
delay(1000L) // delay for 1 second
println("End after delay")
}
Unlike Thread.sleep(), delay() does not block the thread, allowing other coroutines to run during the delay, making asynchronous programming more efficient.