CodeGym /Courses /Kotlin SELF /CoroutineScope and Job — task hierarchy and lifecycle man...

CoroutineScope and Job — task hierarchy and lifecycle management

Kotlin SELF
Level 53, Lesson 0
Available

1. Introduction

When you start writing coroutines, a thought quickly pops up: “Oh, awesome! I’ll parallelize everything!” — and that is a normal stage. But very soon a question comes up that sounds boring, yet saves your nerves: who is responsible for these tasks, and when are they supposed to finish?

Imagine your program is a kitchen. main() is you, and coroutines are assistant chefs. If you just shout “Hey, someone chop the vegetables!” and go to the store, then when you come back you do not know: are the vegetables already chopped, did the assistant get tired and leave, or are they still slicing a tomato alone and crying. Structured concurrency is the rule “chefs work within a shift”: when the shift (operation) ends, all assistants must finish their work, and the kitchen must move to a well-defined state.

In Kotlin this rule is implemented via the idea that coroutines are launched inside CoroutineScope, which defines their lifecycle, and the relationship between tasks forms a “parent → children” hierarchy.

To start with, let’s lock in that today we are not discussing thread switching and Dispatchers, and we are also not diving into cancellation and error handling topics. Our goal is to learn how to package coroutines into manageable boundaries and be able to wait for them to complete.

2. CoroutineScope: the “container” where coroutines live

In plain terms, CoroutineScope is an area within which you are allowed to launch coroutines, and which gives them a shared lifecycle. Technically, launch() and async() are extension functions specifically on CoroutineScope; that is, without a scope you “politely cannot launch them.”

From a beginner developer’s perspective, it helps to keep a simple formula in mind:

“Scope = the place where coroutines are launched and where someone is responsible for them.”

runBlocking { ... } already creates a scope

In console applications, we often start the coroutine world with runBlocking. And it is important to understand: runBlocking does not just “let you call delay,” it also creates a scope and waits for everything launched inside it to finish (within that block). This is a fundamental part of “structuredness.”

A mini example to feel this behavior:


import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

fun main() = runBlocking {
    launch {
        delay(100)
        println("worker done") // worker done
    }
    println("main in runBlocking") // main in runBlocking
}

Here the program will not finish too early: runBlocking will not “let go” of main until the child coroutines launched inside the block have completed.

A small diagram: scope as a boundary

To avoid turning this idea into fog, here is a simple flowchart:

flowchart TD
    A["main()"] --> B["runBlocking { ... } creates a scope"]
    B --> C["launch { ... } child coroutine"]
    B --> D["launch { ... } another child"]
    C --> E["completed"]
    D --> F["completed"]
    E --> G["runBlocking can finish"]
    F --> G

The key point: a scope is not an “abstraction for the sake of abstraction,” but a boundary at which you can honestly say: the work is done.

3. Job: the “handle” for controlling a task’s lifecycle

When you call launch { ... }, Kotlin returns an object of type Job. And this is where beginners often get confused: it feels like if it is “what launch returned,” then it must contain a result. But no: launch is a “coroutine for an action,” and Job is not a result, but a description of the lifecycle.

The documentation directly describes Job as a context element that tracks a coroutine’s lifecycle and helps structure execution.

The most practical action with Job: join()

Job.join() means: “wait until the task completes.”

This is not “synchronization magic,” but a very straightforward command: I want to continue execution only after this coroutine finishes.

import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

fun main() = runBlocking {
    val job: Job = launch {
        delay(100)
        println("task finished") // task finished
    }

    job.join()
    println("after join") // after join
}

Notice a useful habit: if it matters logically that you wait for a coroutine, do it explicitly. Yes, sometimes you can rely on the fact that runBlocking will wait for all children anyway, but join() makes the dependency visible in the code, and readability is an underrated superpower.

Job vs Deferred: do not confuse “lifecycle” with “result”

Here we will carefully reinforce the difference (it was covered the previous day, but today it is critical for clarity).

  • Job — “the task exists / has completed.”
  • Deferred<T> — “the task exists / has completed and also produced a result of type T.”

You can keep a cheat sheet in table form:

What you start How you start it What you get How you wait
Action without a result
launch
Job
join()
Computation with a result
async
Deferred<T>
await()

Important: Deferred<T> is also a task, but today we are interested specifically in the logic of “who is responsible for whom” and “where the completion boundary is,” so the focus is on Job and join().

4. Task hierarchy: parent and children

One of the strongest ideas of structured concurrency in Kotlin sounds like this: nested coroutines form a hierarchy. If you start launch inside another coroutine, it becomes a “child” of the coroutine inside which it was created. The context (including Job) is inherited from the parent by default, and this is how an execution tree is built.

This is nice because the code structure starts reflecting the structure of the tasks’ lives: “this subtask is part of this operation.”

Example: the parent waits for the child automatically

Let’s look at the behavior without an explicit join() on the child:

import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

fun main() = runBlocking {
    val parent = launch {
        launch {
            delay(80)
            println("child done") // child done
        }

        delay(20)
        println("parent work") // parent work
    }

    parent.join()
    println("all finished") // all finished
}

What matters to feel here: we do parent.join(), not child.join(). But parent.join() will not complete until the child coroutine completes as well. That is, the parent is responsible for the children.

The “task tree” as a picture

flowchart TD
    P["parent Job"] --> C1["child Job #1"]
    P --> C2["child Job #2"]

If you read code and see “launch inside launch,” you should automatically think: “okay, this is part of the parent’s operation.” That is practical structured concurrency thinking.

5. coroutineScope { ... }: a scope inside a suspend function

A very common beginner problem: “I want to start several coroutines inside a suspend fun and wait for them to complete. Do I have to write runBlocking again?” No, and moreover, writing runBlocking inside suspend is almost always a bad idea, because you start blocking a thread where you could work asynchronously.

For such cases there is coroutineScope { ... }. It is a builder that creates a new scope inside a suspend function and will complete only when all coroutines launched inside it complete. In Kotlin examples, this is emphasized explicitly: execution continues after coroutineScope once all coroutines inside have finished.

A mini example you can literally memorize as a template:

import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

suspend fun doWorkTogether() = coroutineScope {
    launch { delay(30); println("A") } // A
    launch { delay(10); println("B") } // B
}

And now — a call from main:

import kotlinx.coroutines.runBlocking

fun main() = runBlocking {
    doWorkTogether()
    println("after doWorkTogether") // after doWorkTogether
}

The key meaning of coroutineScope: it is a “structured container” for parallel work inside a suspend function. It helps you avoid scattering launch all over the code, and instead keep parallelism in one place — where you explicitly intended “now there will be several related tasks.”

6. Expense Tracker: one report from parallel steps

So the examples do not live a separate life “in a vacuum,” let’s continue the practical idea of a console application. Suppose we have a conditional expense tracker that can collect expenses in memory and build reports. We are not touching files or the network (those are other days of the course), so we will simulate “long operations” via delay(...).

Today’s goal inside the application is to learn how to build a report so that it consists of several independent parts, yet remains one operation with a clear completion boundary.

Model stub

Let’s make a simple expense model and test data. This is a small base so there is something to “analyze”:

data class Expense(val category: String, val amount: Int)

fun demoExpenses(): List<Expense> = listOf(
    Expense("food", 1200),
    Expense("transport", 300),
    Expense("food", 450),
)

Report subtasks as suspend functions

Let the report consist of two parts: total amount and number of entries. Each part is “slow” (conditionally), so we use delay.

import kotlinx.coroutines.delay

suspend fun calcTotal(expenses: List<Expense>): Int {
    delay(80)
    return expenses.sumOf { it.amount }
}

suspend fun calcCount(expenses: List<Expense>): Int {
    delay(50)
    return expenses.size
}

Now attention: we are not running them in parallel yet. We only made two separate operations that we will combine into one structured scope.

Assemble one operation from multiple coroutines inside coroutineScope

Here is the key moment of the lecture: we want to run two parts of the report in parallel, but in such a way that the “build report” function does not return early. This is exactly a case for coroutineScope {}.

Let’s make a version where we use launch and write results into var. Yes, this is mutable state — but in our educational example it is safe, because we will wait for both coroutines to complete and read the variables only after that. (We will talk about stricter approaches to shared state on other days; today we care about the structure of task lifecycles.)

import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch

suspend fun buildShortReport(expenses: List<Expense>): String = coroutineScope {
    var total = 0
    var count = 0

    val totalJob = launch { total = calcTotal(expenses) }
    val countJob = launch { count = calcCount(expenses) }

    totalJob.join()
    countJob.join()

    "count=$count, total=$total"
}

Two ideas matter here at once:

First, coroutineScope guarantees that we will not “exit outward” until the children have completed. This is structured concurrency in action, and this is exactly how Kotlin proposes building parallel steps as a single operation.

Second, we see Job “live”: launch returned a Job, and join() became an explicit wait point.

Hook it up to main() and look at the result

import kotlinx.coroutines.runBlocking

fun main() = runBlocking {
    val expenses = demoExpenses()
    val report = buildShortReport(expenses)
    println(report) // count=3, total=1950
}

We have gained a small but important skill: building an “operation with internal parallel steps” without leaking coroutines outward.

When join() is actually needed

Now for a moment of honesty: sometimes join() inside coroutineScope seems unnecessary, because “well, coroutineScope already waits for the children.” And that is true: coroutineScope itself will complete when all coroutines inside complete.

But join() remains useful when you need an exact synchronization point in the middle of an operation. For example, you want to wait for step A, then do step B, while step C is running in parallel.

Let’s show an example in the context of our report: suppose we want to know total first, then form the “budget check” line, and count the number of entries in parallel.

import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch

suspend fun buildBudgetReport(expenses: List<Expense>): String = coroutineScope {
    var total = 0
    var count = 0

    val totalJob = launch { total = calcTotal(expenses) }
    val countJob = launch { count = calcCount(expenses) }

    totalJob.join()
    val budgetLine = if (total > 1500) "budget: high" else "budget: ok"

    countJob.join()
    "count=$count, total=$total, $budgetLine"
}

Here join() is not a “crutch,” but part of the logic: we need total to choose the text. And we explicitly place the barrier exactly where it is needed.

7. Common mistakes

Mistake #1: launching coroutines “into thin air” without a clear CoroutineScope.
Sometimes a beginner thinks a coroutine is just “a function that runs by itself.” Then launches appear in the code that are not tied to an operation: it is unclear who is waiting for them and who is responsible for completion. The right habit is to launch coroutines only inside a scope that is an operation boundary: in the console this is often runBlocking, and inside suspend code it is coroutineScope. The very fact that launch and async are started as extension functions of CoroutineScope is not accidental: it is a language hint “do not start a task without an owner.”

Mistake #2: confusing “Job = result”.
A very common scenario: a student writes val x = launch { ... } and then tries to use x as a computed value. But launch returns a Job, and Job is a “lifecycle,” not data. If you need a result, you choose async and await(), and you use Job to wait for completion via join().

Mistake #3: expecting “magical ordering” without join().
Parallelism is tricky because console output and the order of actions become unpredictable if you do not set wait points. If logically you need “wait first, then continue,” that should be expressed in code. The simplest and most honest tool today is job.join(). And even if in a specific place you could rely on the outer scope waiting for all tasks, an explicit join() often makes the program clearer.

Mistake #4: trying to use runBlocking inside a suspend function.
Such a solution sometimes looks like “well, I need to wait.” But runBlocking blocks the thread and inserts a “synchronous boundary” where coroutines should remain asynchronous. For structured concurrency inside suspend code, use coroutineScope, which creates a managed boundary and completes only after all child coroutines complete.

1
Task
Kotlin SELF, level 53, lesson 0
Locked
Completion signal
Completion signal
1
Task
Kotlin SELF, level 53, lesson 0
Locked
Two results
Two results
1
Task
Kotlin SELF, level 53, lesson 0
Locked
Team and intern
Team and intern
1
Task
Kotlin SELF, level 53, lesson 0
Locked
Assembling the tagline
Assembling the tagline
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION