Suspend Functions
In this page:
Declaring a Suspend Function
Adding suspend before fun marks a function as one that can pause execution at certain points and resume later, without ever blocking the thread it runs on.
Warning: This example uses kotlinx.coroutines, which is not part of the plain Kotlin standard library. It requires the kotlinx-coroutines-core dependency (added via Gradle/Maven) to compile and run.
Example: Declaring a Suspend Function
import kotlinx.coroutines.*
suspend fun fetchGreeting(): String {
delay(50)
return "Hello from a suspend function"
}
fun main() = runBlocking {
println(fetchGreeting())
}
Login to try C/C++/Java/PHP code in the editor
Suspend Functions Can Only Be Called from Coroutines
A suspend function cannot be called from ordinary code directly -- it must be called from another suspend function or from inside a coroutine builder such as launch or runBlocking.
Warning: This example uses kotlinx.coroutines, which is not part of the plain Kotlin standard library. It requires the kotlinx-coroutines-core dependency (added via Gradle/Maven) to compile and run.
Example: Suspend Functions Can Only Be Called from Coroutines
import kotlinx.coroutines.*
suspend fun step1(): String {
delay(20)
return "step1 done"
}
suspend fun step2(): String {
delay(20)
return "step2 done"
}
fun main() = runBlocking {
println(step1())
println(step2())
}
Login to try C/C++/Java/PHP code in the editor
delay() vs Thread.sleep()
delay() suspends the coroutine without blocking its underlying thread, allowing that thread to do other work meanwhile, unlike Thread.sleep() which blocks the entire thread outright.
Warning: This example uses kotlinx.coroutines, which is not part of the plain Kotlin standard library. It requires the kotlinx-coroutines-core dependency (added via Gradle/Maven) to compile and run.
Example: delay() vs Thread.sleep()
import kotlinx.coroutines.*
fun main() = runBlocking {
launch {
delay(100)
println("Task A finished")
}
println("Main coroutine continues immediately")
}
Login to try C/C++/Java/PHP code in the editor
Composing Suspend Functions
Suspend functions can call other suspend functions directly, letting you build up sequential asynchronous logic that reads just like ordinary, synchronous-looking code.
Warning: This example uses kotlinx.coroutines, which is not part of the plain Kotlin standard library. It requires the kotlinx-coroutines-core dependency (added via Gradle/Maven) to compile and run.
Example: Composing Suspend Functions
import kotlinx.coroutines.*
suspend fun fetchUser(): String {
delay(30)
return "Ana"
}
suspend fun greetUser(): String {
val name = fetchUser()
return "Hello, $name!"
}
fun main() = runBlocking {
println(greetUser())
}
Login to try C/C++/Java/PHP code in the editor
- Trying to call a
suspendfunction from ordinary, non-suspending code without a coroutine builder or another suspend function to call it from. - Assuming
suspendmakes a function automatically run on a background thread; it only marks that it *can* suspend, the actual threading depends on the dispatcher used. - Forgetting that suspension points (like
delay) only actually pause execution, they don't block the underlying thread the coroutine is running on.
- The
suspendkeyword marks a function that can pause its execution and resume later without blocking a thread. - A
suspendfunction can only be called from anothersuspendfunction or from within a coroutine builder likelaunch/runBlocking. delay()is a suspend function that pauses a coroutine without blocking the underlying thread, unlikeThread.sleep().- Marking a function
suspenddoes not, by itself, move its work to a background thread.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: