Skip to main content

delay in Coroutines in Kotlin

2 min read
Share:
On this page (12sections)

Introduction

delay in Coroutines 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.

Delay suspends coroutine for given time without blocking thread. 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

  • delay suspends coroutine for given time without blocking thread.
  • Time unit is milliseconds by default.
  • Other work can run on same or other threads during delay.

Syntax

delay(timeMillis)

delay in Coroutines in Kotlin Example Program in Kotlin

import kotlinx.coroutines.*

fun main(args: Array<String>) = runBlocking {
    println("Start")
    delay(100)
    println("End")
}

Sample Output

Start
End

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

  1. The program starts with a main function — the entry point that runs when you execute the file.

  2. The println("Start") statement writes a line to the console — this produces part of the sample output below.

  3. The println("End") statement writes a line to the console — this produces part of the sample output below.

  4. Delay suspends coroutine for given time without blocking thread.

  5. 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 suspend functions and appropriate dispatchers.
  • Scope coroutines with coroutineScope or supervisorScope for structured cancellation.
  • Use delay instead of Thread.sleep inside coroutines.

Common Mistakes

  • Launching coroutines without a scope — leaks work after the UI is destroyed.
  • Using GlobalScope in application code instead of a lifecycle-aware scope.
  • Calling blocking APIs directly on Dispatchers.Main.

Key Points

  • delay suspends coroutine for given time without blocking thread.
  • Time unit is milliseconds by default.
  • Other work can run on same or other threads during delay.
  • 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-core dependency when running coroutine examples outside Android or IntelliJ.
  • Semicolons at the end of statements are optional in Kotlin.

Frequently Asked Questions

What is delay in Coroutines in Kotlin?
Delay suspends coroutine for given time without blocking thread.
When should I use delay in Coroutines?
Use coroutines for network requests, database queries, or any work that would block the main thread if done synchronously.
How is delay in Coroutines different from Java?
Other work can run on same or other threads during delay.
How do I practice this topic?
Copy the example program into IntelliJ IDEA or Android Studio, run it, then modify values or add print statements to confirm your understanding.

Related Tutorials

Search tutorials