runBlocking in Kotlin
On this page (12sections)
Introduction
runBlocking is a fundamental concept every Kotlin developer should understand. Coroutines let you write asynchronous code that reads like synchronous code, without blocking threads or nesting callbacks.
RunBlocking bridges blocking code with coroutines. In this tutorial you will learn the syntax, walk through a complete example program, study the sample output, and review best practices so you can apply the concept confidently in your own projects.
Definition
- runBlocking bridges blocking code with coroutines.
- It blocks current thread until coroutine completes.
- Commonly used in main function for examples and tests.
Syntax
runBlocking { }
runBlocking in Kotlin Example Program in Kotlin
import kotlinx.coroutines.*
fun main(args: Array<String>) = runBlocking {
println("Before delay")
delay(100)
println("After delay")
}
Sample Output
Before delay
After delay
When to use
Use coroutines for network requests, database queries, or any work that would block the main thread if done synchronously.
How it works
-
The program starts with a
mainfunction — the entry point that runs when you execute the file. -
The
println("Before delay")statement writes a line to the console — this produces part of the sample output below. -
The
println("After delay")statement writes a line to the console — this produces part of the sample output below. -
RunBlocking bridges blocking code with coroutines.
-
Run the program in IntelliJ IDEA, Android Studio, or with the Kotlin command-line compiler (
kotlinc/kotlin). Compare your console output with the sample output shown below.
Best Practices
- Never block the main thread — use
suspendfunctions and appropriate dispatchers. - Scope coroutines with
coroutineScopeorsupervisorScopefor structured cancellation. - Use
delayinstead ofThread.sleepinside coroutines.
Common Mistakes
- Launching coroutines without a scope — leaks work after the UI is destroyed.
- Using
GlobalScopein application code instead of a lifecycle-aware scope. - Calling blocking APIs directly on
Dispatchers.Main.
Key Points
- runBlocking bridges blocking code with coroutines.
- It blocks current thread until coroutine completes.
- Commonly used in main function for examples and tests.
- Test the example locally and verify the output matches the sample.
- Experiment by changing input values to see how behaviour changes.
Notes
- Add the
kotlinx-coroutines-coredependency when running coroutine examples outside Android or IntelliJ. - Semicolons at the end of statements are optional in Kotlin.
Frequently Asked Questions
What is runBlocking in Kotlin?
When should I use runBlocking?
How is runBlocking different from Java?
How do I practice this topic?
Related Tutorials
launch Coroutine in Kotlin
Learn launch Coroutine in Kotlin with clear explanation, syntax, example program, sample output, best practices, and FAQs.
Read tutorialCoroutine Scope in Kotlin
Learn Coroutine Scope in Kotlin with clear explanation, syntax, example program, sample output, best practices, and FAQs.
Read tutorial