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

相关推荐
zh_xuan2 小时前
kotlin 类继承的语法
开发语言·kotlin
冬奇Lab5 小时前
【Kotlin系列02】变量与数据类型:从val/var到空安全的第一课
android·kotlin·编程语言
资生算法程序员_畅想家_剑魔18 小时前
Kotlin常见技术分享-02-相对于Java 的核心优势-协程
java·开发语言·kotlin
aqi001 天前
FFmpeg开发笔记(九十八)基于FFmpeg的跨平台图形用户界面LosslessCut
android·ffmpeg·kotlin·音视频·直播·流媒体
冬奇Lab1 天前
【Kotlin系列01】Kotlin快速入门:环境搭建与Hello World
android·kotlin·android studio
儿歌八万首2 天前
Jetpack Compose 动画实战:让你的 UI 动起来
android·kotlin·动画·compose
stevenzqzq2 天前
Android Koin 注入入门教程
android·kotlin
zFox2 天前
三、Kotlin协程+异步加载+Loading状态
kotlin·android jetpack·协程