LoginSignup
1
0

More than 3 years have passed since last update.

IntelliJ IDEA Coroutineのデバッグを有効化

Last updated at Posted at 2019-07-01

デバッグを有効化する

Kotlin Coroutineを学習中でデバッグ機能を有効化したいが設定できない、
そんなときは次の手順で設定すると有効化できる。

  1. 「IntteliJ IDEA」->「Run」->「Edit Configurations」を開く
  2. 「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
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0