Skip to content
Learn Netverks

Lesson

Step 25/36 69% through track

coroutines-intro

Coroutines introduction

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~2 min
Level
advanced

This lesson

An orientation to the Kotlin track—how the compiled playground works, core vocabulary, and what you will practice next.

You need a clear map of the Kotlin track so null safety, data classes, coroutines, and JVM interop do not feel like magic.

You will apply Coroutines introduction in contexts like: Network calls, database I/O, and UI state on Android and Ktor servers.

Write Kotlin in main.kt with fun main(), click Run on server—the dev runner kotlinc compiles to a JVM jar and java runs it; use println for output (requires JDK + kotlinc; LEARNING_RUNNER_ENABLED=true). Also read the interview prep blocks.

After the Java track or strong Java experience—Kotlin is designed as a better Java for JVM and Android teams.

Coroutines are lightweight concurrent tasks designed for structured async I/O—Kotlin's alternative to callback chains in JavaScript and thread pools in Java. On Gradle projects you use kotlinx-coroutines; the playground simulates concurrency with threads.

Concepts

  • suspend functions can pause without blocking a thread
  • CoroutineScope and structured concurrency cancel child work when parents fail
  • async/await and launch schedule work on dispatchers

runBlocking (local / Gradle only)

In Android Studio or a Gradle module with coroutines on the classpath, runBlocking { } bridges blocking main to suspend functions for demos and tests. Do not use runBlocking on production request threads—prefer suspend all the way up.

// Local Gradle — not in playground
runBlocking {
    val result = fetchUser()
    println(result)
}

This lesson's runnable code uses kotlin.concurrent.thread and Thread.sleep to preview overlapping work without the coroutines library.

Important interview questions and answers

  1. Q: Coroutine vs thread?
    A: Coroutines are cooperatively scheduled on thread pools—cheaper to spawn thousands for I/O-bound work; threads are OS resources with higher overhead.
  2. Q: What is runBlocking for?
    A: Blocking entry point that runs a coroutine scope to completion—fine for main and tests, not for reactive server threads.

Self-check

  1. Why avoid runBlocking on a servlet thread?
  2. What keyword marks a function that can suspend?

Tip: Real projects add kotlinx-coroutines and use runBlocking only in main or tests—not on HTTP worker threads.

Interview prep

runBlocking in servers?

Avoid on request threads—blocks and hurts scalability.

Coroutines vs threads?

Lightweight tasks with suspend; structured cancellation.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • suspend meaning?
  • Dispatcher choice?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump