Last Updated | 17 September 2020 |
The tutorial assumes you have prior knowledge of the coroutines concept.
Debugging works for
kotlinx-coroutines-core
version 1.3.8 or later.
Open a Kotlin project in IntelliJ IDEA. If you don't have a project, create one.
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!
.
Change code in the main()
function:
runBlocking()
block to wrap a coroutine.async()
function to create coroutines that compute deferred values a
and b
.await()
function to await the computation result.println()
function to print computing status and the result of multiplication to the output.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()}") }
Build the code by clicking Build Project.
Set breakpoints at the lines with the println()
function call:
Run the code in debug mode by clicking Debug next to the run configuration at the top of the screen.
The Debug tool window appears:
Resume the debugger session by clicking Resume program in the Debug tool window:
Now the Coroutines tab shows the following:
a
value – it has the RUNNING status.b
.Resume the debugger session by clicking Resume program in the Debug tool window:
Now the Coroutines tab shows the following:
b
– it has the RUNNING status.Using IntelliJ IDEA debugger, you can dig deeper into each coroutine to debug your code.
© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/docs/tutorials/coroutines/debug-coroutines-with-idea.html