← Back to Kotlin Course | Chapter 12: Coroutines | Lesson 1 of 7

Coroutine Basics

A coroutine is a lightweight task that Kotlin can pause and resume later, letting your program do other things while it waits.

What Is a Coroutine?

A coroutine is a lightweight unit of concurrent work that can be paused and resumed without blocking an underlying operating system thread, letting many coroutines share a small pool of real threads.

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: What Is a Coroutine?

markup
import kotlinx.coroutines.*

fun main() = runBlocking {
    println("Starting a coroutine")
    launch {
        println("Hello from a coroutine!")
    }
    println("Coroutine launched")
}

runBlocking as an Entry Point

runBlocking { } creates a coroutine scope and blocks the calling thread until everything inside completes, which is why it is commonly used to wrap main() in coroutine examples.

Note: Avoid runBlocking inside real application code paths; it is best suited for main() functions and tests.

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: runBlocking as an Entry Point

markup
import kotlinx.coroutines.*

fun main() = runBlocking {
    delay(100)
    println("Finished after a delay, inside runBlocking")
}

Coroutines Are Not Threads

Unlike threads, which are relatively expensive operating system resources, coroutines are cheap and lightweight -- you can run tens of thousands of them concurrently on a handful of actual threads.

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: Coroutines Are Not Threads

markup
import kotlinx.coroutines.*

fun main() = runBlocking {
    repeat(5) { i ->
        launch {
            println("Coroutine $i running")
        }
    }
}

Why kotlinx.coroutines Is a Library, Not the Standard Library

The core coroutine building blocks (suspend functions) are part of the Kotlin language, but the practical tools like launch, async, and Dispatchers come from the separate kotlinx.coroutines library, added to a project via Gradle or Maven.

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: Why kotlinx.coroutines Is a Library, Not the Standard Library

markup
import kotlinx.coroutines.*

fun main() = runBlocking {
    println("This program depends on kotlinx-coroutines-core")
}
Common Mistakes
  1. Confusing a coroutine with a full operating-system thread; thousands of coroutines can run on just a few real threads.
  2. Forgetting that kotlinx.coroutines is a separate library, not part of the plain Kotlin standard library, so it must be added as a project dependency.
  3. Calling runBlocking inside real application code as a habit from examples, when it blocks the current thread and defeats the purpose of using coroutines there.
Chapter Summary
  • A coroutine is a lightweight, suspendable computation, not a full operating-system thread.
  • runBlocking starts a coroutine and blocks the current thread until it completes, mainly used in main() functions and tests.
  • The kotlinx.coroutines library provides coroutine support and must be added as a dependency; it is not part of Kotlin's standard library.
  • Coroutines make concurrent code look sequential, avoiding deeply nested callbacks.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.