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

相关推荐
alexhilton2 小时前
Compose中的ContentScale:终极可视化指南
android·kotlin·android jetpack
jzlhll1233 小时前
kotlin Flow first() last()总结
开发语言·前端·kotlin
符哥20087 小时前
充电桩 WiFi 局域网配网(Android/Kotlin)流程、指令及实例说明文档
android·开发语言·kotlin
大傻^11 小时前
SpringAI2.0 Null Safety 实战:JSpecify 注解体系与 Kotlin 互操作
android·开发语言·人工智能·kotlin·springai
jzlhll12315 小时前
Kotlin Mutex vs Java ReentrantLock vs synchronized
java·开发语言·kotlin
Kapaseker15 小时前
一杯 Kotlin 美式品味 object 声明
android·kotlin
俩个逗号。。16 小时前
Kotlin 扩展函数详解
开发语言·kotlin
su1ka1111 天前
Kotlin(3)基本语法
kotlin
su1ka1112 天前
Kotlin(4)面向对象
kotlin
鹧鸪晏2 天前
搞懂 kotlin 泛型 out 和 in 关键字
android·kotlin