Skip to content
GitLab
Menu
Why GitLab
Pricing
Contact Sales
Explore
Why GitLab
Pricing
Contact Sales
Explore
Sign in
Get free trial
Changes
Page history
Update Kotlin coroutines
authored
Jul 31, 2023
by
umaumax
Show whitespace changes
Inline
Side-by-side
Kotlin-coroutines.md
View page @
dbaeefdf
...
...
@@ -228,6 +228,20 @@ for (i in 1..100) {
c++のように、mutexなどを利用したlock/unlockは不要であるが、切り替えタイミングによって、値が意図せずに変更される可能性があることに注意する必要がある
### 実行しているコルーチンの中におけるコルーチンのキャンセル状態の監視
[
android - How to check Kotlin coroutine job.isActive in the calling method - Stack Overflow
](
https://stackoverflow.com/questions/62816696/how-to-check-kotlin-coroutine-job-isactive-in-the-calling-method
)
キャンセルを受け付けるタイミングで、定期的に、
`yield()`
か
`ensureActive()`
を呼び出す
`isActive`
を利用するコードも見られるが、
`ensureActive()`
を呼び出す方が適切な処理である
[
ensureActive
](
https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/ensure-active.html
)
```
kotlin
if
(!
isActive
)
{
throw
CancellationException
()
}
```
### コルーチンの並列度
rustのtokioと同じように明示的に別のスレッドを利用しない限り、コルーチンの同時実行数は1である
...
...
...
...