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

相关推荐
灯火不休ᝰ13 小时前
[kotlin] 从Java到Kotlin:掌握基础语法差异的跃迁指南
java·kotlin·安卓
モンキー・D・小菜鸡儿19 小时前
kotlin 推牌九(麻将)小游戏
kotlin·小游戏
JMchen12320 小时前
跨平台相机方案深度对比:CameraX vs. Flutter Camera vs. React Native
java·经验分享·数码相机·flutter·react native·kotlin·dart
DokiDoki之父2 天前
边写软件边学kotlin(一):Kotlin语法初认识:
android·开发语言·kotlin
fundroid3 天前
Kotlin 泛型进阶:in、out 与 reified 实战
android·开发语言·kotlin
JMchen1233 天前
现代Android图像处理管道:从CameraX到OpenGL的60fps实时滤镜架构
android·图像处理·架构·kotlin·android studio·opengl·camerax
JMchen1235 天前
Android CameraX深度解析:从Camera1到CameraX的相机架构演进
android·java·数码相机·架构·kotlin·移动开发·android-studio
倔强的石头1065 天前
【Linux指南】进程控制系列(五)实战 —— 微型 Shell 命令行解释器实现
linux·运维·kotlin
Hz4535 天前
Android Jetpack核心组件协同实战:Navigation 3.X+Lifecycle+Flow+Hilt的架构革新
android·kotlin
JMchen1236 天前
Android音频编码原理与实践:从AAC到Opus,深入解析音频编码技术与移动端实现
android·经验分享·学习·kotlin·android studio·音视频·aac