aboutsummaryrefslogtreecommitdiffstats
path: root/docs/topics/debug-coroutines-with-idea.md
blob: e59075e071112daca67dbe2afe680d7009c08887 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
[//]: # (title: Debug coroutines using IntelliJ IDEA – tutorial)

This tutorial demonstrates how to create Kotlin coroutines and debug them using IntelliJ IDEA.

The tutorial assumes you have prior knowledge of the [coroutines](coroutines-guide.md) concept.

> Debugging works for `kotlinx-coroutines-core` version 1.3.8 or later.
>
{type="note"}

## Create coroutines

1. Open a Kotlin project in IntelliJ IDEA. If you don't have a project, [create one](jvm-get-started.md#create-an-application).

2. Open the `main.kt` file in `src/main/kotlin`.

    The `src` directory contains Kotlin source files and resources. The `main.kt` file contains sample code that will print `Hello World!`.

2. Change code in the `main()` function:

    * Use the [`runBlocking()`](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/run-blocking.html) block to wrap a coroutine.
    * Use the [`async()`](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/async.html) function to create coroutines that compute deferred values `a` and `b`.
    * Use the [`await()`](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-deferred/await.html) function to await the computation result.
    * Use the [`println()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/println.html) function to print computing status and the result of multiplication to the output.

    ```kotlin
    import kotlinx.coroutines.*
    
    fun main() = runBlocking<Unit> {
        val a = async {
            println("I'm computing part of the answer")
            6
        }
        val b = async {
            println("I'm computing another part of the answer")
            7
        }
        println("The answer is ${a.await() * b.await()}")
    }
    ```

4. Build the code by clicking **Build Project**.

    ![Build an application](flow-build-project.png)

## Debug coroutines

1. Set breakpoints at the lines with the `println()` function call:

    ![Build a console application](coroutine-breakpoint.png)

2. Run the code in debug mode by clicking **Debug** next to the run configuration at the top of the screen.

    ![Build a console application](flow-debug-project.png)

    The **Debug** tool window appears: 
    * The **Frames** tab contains the call stack.
    * The **Variables** tab contains variables in the current context.
    * The **Coroutines** tab contains information on running or suspended coroutines. It shows that there are three coroutines.
    The first one has the **RUNNING** status, and the other two have the **CREATED** status.

    ![Debug the coroutine](coroutine-debug-1.png)

3. Resume the debugger session by clicking **Resume program** in the **Debug** tool window:

    ![Debug the coroutine](coroutine-debug-2.png)
    
    Now the **Coroutines** tab shows the following:
    * The first coroutine has the **SUSPENDED** status – it is waiting for the values so it can multiply them.
    * The second coroutine is calculating the `a` value – it has the **RUNNING** status.
    * The third coroutine has the **CREATED** status and isn’t calculating the value of `b`.

4. Resume the debugger session by clicking **Resume program** in the **Debug** tool window:

    ![Build a console application](coroutine-debug-3.png)

    Now the **Coroutines** tab shows the following:
    * The first coroutine has the **SUSPENDED** status – it is waiting for the values so it can multiply them.
    * The second coroutine has computed its value and disappeared.
    * The third coroutine is calculating the value of `b` – it has the **RUNNING** status.

Using IntelliJ IDEA debugger, you can dig deeper into each coroutine to debug your code.