デバッグを有効化する
Kotlin Coroutineを学習中でデバッグ機能を有効化したいが設定できない、
そんなときは次の手順で設定すると有効化できる。
- 「IntteliJ IDEA」->「Run」->「Edit Configurations」を開く
- 「VM options」に「-Dkotlinx.coroutines.debug」を入力し「OK」を押す
有効化してサンプルを実行してみる
それじゃ試しに設定した後にDebugging coroutines and threadsのサンプルを実行してみます。
import kotlinx.coroutines.*
fun log(msg: String) = println("[${Thread.currentThread().name}] $msg")
fun main() = runBlocking<Unit>{
val a = async {
log("I'm computing a piece of the answer")
6
}
val b = async {
log("I'm computing another piece of the answer")
7
}
log("The anwer is ${a.await() * b.await()}")
}
デバッグを有効化した場合
有効化したときは@coroutine#2、@coroutine#3が表示されているのでしっかり有効化できています。
[main @coroutine#2] I'm computing a piece of the answer
[main @coroutine#3] I'm computing another piece of the answer
[main @coroutine#1] The anwer is 42
Process finished with exit code 0
デバッグを無効化した場合
無効化したときは@coroutine#2、@coroutine#3が表示されてませんね。
[main] I'm computing a piece of the answer
[main] I'm computing another piece of the answer
[main] The anwer is 42
Process finished with exit code 0