Coroutine Basics
In this page:
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?
import kotlinx.coroutines.*
fun main() = runBlocking {
println("Starting a coroutine")
launch {
println("Hello from a coroutine!")
}
println("Coroutine launched")
}
Login to try C/C++/Java/PHP code in the editor
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
import kotlinx.coroutines.*
fun main() = runBlocking {
delay(100)
println("Finished after a delay, inside runBlocking")
}
Login to try C/C++/Java/PHP code in the editor
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
import kotlinx.coroutines.*
fun main() = runBlocking {
repeat(5) { i ->
launch {
println("Coroutine $i running")
}
}
}
Login to try C/C++/Java/PHP code in the editor
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
import kotlinx.coroutines.*
fun main() = runBlocking {
println("This program depends on kotlinx-coroutines-core")
}
Login to try C/C++/Java/PHP code in the editor
- Confusing a coroutine with a full operating-system thread; thousands of coroutines can run on just a few real threads.
- Forgetting that
kotlinx.coroutinesis a separate library, not part of the plain Kotlin standard library, so it must be added as a project dependency. - Calling
runBlockinginside real application code as a habit from examples, when it blocks the current thread and defeats the purpose of using coroutines there.
- A coroutine is a lightweight, suspendable computation, not a full operating-system thread.
runBlockingstarts a coroutine and blocks the current thread until it completes, mainly used inmain()functions and tests.- The
kotlinx.coroutineslibrary 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: