Kotlin超时withTimeout超时与ensureActive()取消协程任务执行

Kotlin超时withTimeout超时与ensureActive()取消协程任务执行

Kotlin 复制代码
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout

fun main() {
    runBlocking {
        val job = async {
            runCatching {
                withTimeout(4) {
                    repeat(Int.MAX_VALUE) { it ->
                        println("repeat-$it")
                    }
                }
            }.onFailure { e ->
                println("onFailure $e")
            }
        }
    }
}

上面的代码,虽然意图是给几乎无限的repeat任务设置4ms的超时值,4ms后,自动打断repeat任务,并进入onFailure回调。但是,程序运行后:

...

repeat-473038

repeat-473039

repeat-473040

repeat-473041

repeat-473042

repeat-473043

repeat-473044

repeat-473045

...

任务无限的跑下去停不下来,withTimeout超时打断Kotlin协程任务的机制表明失效。

但是,如果给协程增加活动检测ensureActive():

Kotlin 复制代码
import kotlinx.coroutines.async
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout

fun main() {
    runBlocking {
        val job = async {
            runCatching {
                withTimeout(4) {
                    repeat(Int.MAX_VALUE) { it ->

                        this.ensureActive()

                        println("repeat-$it")
                    }
                }
            }.onFailure { e ->
                println("onFailure $e")
            }
        }
    }
}

程序运行后:

repeat-0

repeat-1

repeat-2

repeat-3

repeat-4

repeat-5

repeat-6

repeat-7

repeat-8

repeat-9

repeat-10

repeat-11

repeat-12

repeat-13

repeat-14

repeat-15

repeat-16

repeat-17

repeat-18

repeat-19

repeat-20

repeat-21

repeat-22

repeat-23

repeat-24

repeat-25

repeat-26

repeat-27

repeat-28

repeat-29

repeat-30

repeat-31

repeat-32

repeat-33

repeat-34

repeat-35

repeat-36

repeat-37

repeat-38

repeat-39

repeat-40

repeat-41

repeat-42

repeat-43

repeat-44

repeat-45

repeat-46

repeat-47

repeat-48

repeat-49

repeat-50

repeat-51

repeat-52

repeat-53

repeat-54

repeat-55

repeat-56

repeat-57

repeat-58

repeat-59

repeat-60

repeat-61

repeat-62

repeat-63

repeat-64

repeat-65

repeat-66

repeat-67

repeat-68

repeat-69

repeat-70

repeat-71

repeat-72

repeat-73

repeat-74

repeat-75

repeat-76

repeat-77

repeat-78

repeat-79

repeat-80

onFailure kotlinx.coroutines.TimeoutCancellationException: Timed out waiting for 4 ms

在约定的4ms后,因为超时而被打断,从而任务取消。

相关:

https://blog.csdn.net/zhangphil/article/details/129585728

https://blog.csdn.net/zhangphil/article/details/155615069

https://blog.csdn.net/zhangphil/article/details/132099928

相关推荐
hnlgzb11 小时前
安卓app开发,如何快速上手kotlin和compose的开发?
android·开发语言·kotlin
alexhilton11 小时前
Jetpack Compose 2025年12月版本新增功能
android·kotlin·android jetpack
lin62534221 天前
Android九宫格,1张图到9张图适配;图片自定义UI
android·ui·kotlin
zhangphil1 天前
Kotlin协程buffer缓冲池,调度任务执行
kotlin
モンキー・D・小菜鸡儿2 天前
Android Jetpack Compose 基础控件介绍
android·kotlin·android jetpack·compose
侠***I2 天前
基于OOA-TCN-BiGRU-Attention的鱼鹰算法优化多变量时间序列预测
kotlin
MengFly_2 天前
KotlinInline关键字
kotlin
用户69371750013842 天前
27.Kotlin 空安全:安全转换 (as?) 与非空断言 (!!)
android·后端·kotlin
用户985120035832 天前
KotlinInline关键字
kotlin