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

相关推荐
JMchen1233 小时前
Android后台服务与网络保活:WorkManager的实战应用
android·java·网络·kotlin·php·android-studio
儿歌八万首6 小时前
硬核春节:用 Compose 打造“赛博鞭炮”
android·kotlin·compose·春节
消失的旧时光-19439 小时前
从 Kotlin 到 Dart:为什么 sealed 是处理「多种返回结果」的最佳方式?
android·开发语言·flutter·架构·kotlin·sealed
有位神秘人9 小时前
kotlin与Java中的单例模式总结
java·单例模式·kotlin
Jinkxs9 小时前
Gradle - 与Groovy/Kotlin DSL对比 构建脚本语言选择指南
android·开发语言·kotlin
&有梦想的咸鱼&9 小时前
Kotlin委托机制的底层实现深度解析(74)
android·开发语言·kotlin
golang学习记9 小时前
IntelliJ IDEA 2025.3 重磅发布:K2 模式全面接管 Kotlin —— 告别 K1,性能飙升 40%!
java·kotlin·intellij-idea
gjxDaniel2 天前
Kotlin编程语言入门与常见问题
android·开发语言·kotlin
野生技术架构师2 天前
Java 21虚拟线程 vs Kotlin协程:高并发编程模型的终极对决与选型思考
java·开发语言·kotlin
言之。2 天前
Kotlin快速入门
android·开发语言·kotlin