ProgramGuru

Coroutines and suspending functions

Coroutines and suspending functions

A report might wait for data while other work progresses. A suspending function can pause a coroutine without requiring its thread to remain blocked for that wait. Suspend marks a capability, not a promise to start a new coroutine or choose a background thread.

What you’ll learn

  • Trace a suspending call and its result.
  • Identify the coroutine library dependency.

Coroutines and suspending functions in the example

  1. Enter coroutine

    A report might wait for data while other work progresses.

    Enter coroutine
  2. Suspend for simulated wait

    The examples use kotlinx.coroutines, a separate library.

    Suspend for simulated wait
  3. Use completed result

    Delay is a tiny simulated wait, not a network request or timing guarantee.

    Use completed result
Delay is a tiny simulated wait, not a network request or timing guarantee. The coroutine obtains 80 before displaying the score, so the transcript order follows the dependency. Other work could run during suspension if it were scheduled in the surrounding context.

Understand coroutines and suspending functions

The examples use kotlinx.coroutines, a separate library. Add its JVM dependency to a local project or provide the JAR on the compiler and runtime classpaths. RunBlocking bridges this command-line entry point and suspending work by blocking its caller; it is not a general UI-thread pattern.

Run a library-based example locally

For a configured Gradle project, declare the JVM coroutine dependency shown in the example. For command-line practice, download the coroutine JVM JAR as coroutines.jar. Compile with kotlinc Main.kt -cp coroutines.jar -include-runtime -d school.jar and run java -cp school.jar:coroutines.jar MainKt. Windows uses a semicolon instead of the classpath colon. The online compiler’s ordinary single-file environment does not promise this extra dependency.

Connect this to functions and return values and gradle projects and boundary tests.

Follow coroutines and suspending functions

Save the complete source as Main.kt and use the Kotlin/JVM environment. Use a local Kotlin/JVM project with org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.2. This example requires the library on both compile and runtime classpaths. See coroutine setup.

Kotlin/JVM · complete program

import kotlinx.coroutines.*
suspend fun loadScore(): Int {
  delay(1)
  return 80
}
fun main() = runBlocking {
  println("Before request")
  val score = loadScore()
  println("Score: $score")
  println("After result")
}

Expected output

Before request
Score: 80
After result

Follow the execution

Delay is a tiny simulated wait, not a network request or timing guarantee. The coroutine obtains 80 before displaying the score, so the transcript order follows the dependency. Other work could run during suspension if it were scheduled in the surrounding context.

Practice: Coroutines and suspending functions

Your task: Does suspend by itself launch work on a new thread?

Kotlin/JVM · complete program

import kotlinx.coroutines.*
suspend fun loadScore(): Int {
  delay(1)
  return 80
}
fun main() = runBlocking {
  println("Before request")
  val score = loadScore()
  println("Score: $score")
  println("After result")
}

Worked answer

No. The caller, context and scheduling APIs determine execution. A suspending declaration only permits suspension under its contract.

Coroutines and suspending functions: the important boundary

A blocking library call does not become non-blocking merely because it sits inside suspend. Choose an appropriate context for real blocking work and handle errors and cancellation. These library examples need the documented dependency.

Check your understanding

Does suspend by itself launch work on a new thread?
Choose one answer. You can try again.

Answer and explanation

No.

No. The caller, context and scheduling APIs determine execution. A suspending declaration only permits suspension under its contract.

Coroutines and suspending functions: what to keep in mind

No. The caller, context and scheduling APIs determine execution. A suspending declaration only permits suspension under its contract. A blocking library call does not become non-blocking merely because it sits inside suspend.

Further reading: official Kotlin documentation for this topic.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Structured concurrency and cancellation →