Skip to content
Update Kotlin coroutines authored by umaumax's avatar umaumax
......@@ -364,6 +364,38 @@ e.g. `java -Dkotlinx.coroutines.debug -jar ./app/build/libs/app-jvm.jar`
> launch {} function で Coroutine を開始でき、コードブロック内に並列に実行する処理を記述できます。
### Error: Main method must return a value of type void
``` bash
Error: Main method must return a value of type void in class com.example.AppKt, please
define the main method as:
public static void main(String[] args)
```
発生例
``` kotlin
package com.example.app
import kotlinx.coroutines.*
fun main(args: Array<String>) = runBlocking {
var job = launch {
withContext(Dispatchers.IO) {
delay(50)
println("C start")
delay(100)
}
}
delay(100)
job?.cancelAndJoin() // jobはnullableではないので、そもそも`?`が不要である
Unit
}
```
`job.cancelAndJoin()`とすればビルドが通る
これは`job?`の場合に返り値が`null`となってしまうので、`Unit`が不足すると`main`関数の返り値として、`null`を指定していることとなるため、エラーが発生する
## Q&A
### サードパーティのライブラリのメソッドがasync/awaitに対応しているのかどうかはどうやって判断する?
* よくあるライブラリの利用方法を見る
......
......