Kotlin->Kotlin协程的取消机制

取消整个协程作用域CoroutineScope

kotlin 复制代码
Log.i("yang", "Main Start")
val mScope = CoroutineScope(Dispatchers.IO)

val mJob1 = mScope.launch {
    try {
        Log.i("yang", "Job1 Start")
        delay(1000)
        Log.i("yang", "Job1 End")
    } catch (e: Exception) {
        Log.i("yang", "Job1 Cancelled e = ${e.message}")
    }
}

val mJob2 = mScope.launch {
    try {
        Log.i("yang", "Job2 Start")
        delay(2000)
        Log.i("yang", "Job2 End")
    } catch (e: Exception) {
        Log.i("yang", "Job2 Cancelled e = ${e.message}")
    }
}

Log.e("yang", "Main Continue")
Thread.sleep(500)

mScope.cancel()
Log.e("yang", "Main Cancel")
Log.i("yang", "Main End")

// log
2025-01-06 17:36:52.588  1752-1752  yang                    com.example.myapplication            I  Main Start
2025-01-06 17:36:52.593  1752-1752  yang                    com.example.myapplication            E  Main Continue
2025-01-06 17:36:52.593  1752-2367  yang                    com.example.myapplication            I  Job1 Start
2025-01-06 17:36:52.594  1752-2391  yang                    com.example.myapplication            I  Job2 Start
2025-01-06 17:36:53.094  1752-1752  yang                    com.example.myapplication            E  Main Cancel
2025-01-06 17:36:53.094  1752-1752  yang                    com.example.myapplication            I  Main End
2025-01-06 17:36:53.094  1752-2367  yang                    com.example.myapplication            I  Job1 Cancelled e = Job was cancelled
2025-01-06 17:36:53.094  1752-2407  yang                    com.example.myapplication            I  Job2 Cancelled e = Job was cancelled
  • IO线程创建一个协程作用域CoroutineScope(Dispatchers.IO)依次启动两个协程任务,主线程在500ms后取消整个协程作用域,其中的两个协程任务都会取消
  • 主线程在取消协程作用域mScope.cancel()之后,其中两个JobStart语句会执行,End语句不会执行,因为取消协程作用域时,两个Job都抛出了异常

取消没有返回值的协程任务Job

withContext挂起函数检测取消信号

kotlin 复制代码
Log.i("yang", "Main Start")
val mJob = CoroutineScope(Dispatchers.IO).launch {
    try {
        Log.i("yang", "IO Start")
        Thread.sleep(1000) // 模拟耗时任务
        Log.i("yang", "IO End")
        withContext(Dispatchers.Main) {
            Log.i("yang", "Main withContext Start")
        }
    } catch (e: Exception) {
        Log.i("yang", "Launch Exception = ${e.message}")
    }
}
Log.i("yang", "Main Continue")
Thread.sleep(500)
mJob.cancel()
Log.i("yang", "Main End")

// log
2025-01-06 16:13:46.784 29337-29337 yang                    com.example.myapplication            I  Main Start
2025-01-06 16:13:46.785 29337-29337 yang                    com.example.myapplication            I  Main Continue
2025-01-06 16:13:46.785 29337-29622 yang                    com.example.myapplication            I  IO Start
2025-01-06 16:13:47.285 29337-29337 yang                    com.example.myapplication            I  Main End
2025-01-06 16:13:47.785 29337-29622 yang                    com.example.myapplication            I  IO End
2025-01-06 16:13:47.785 29337-29622 yang                    com.example.myapplication            I  Launch Exception = StandaloneCoroutine was cancelled
  • IO线程创建一个协程作用域CoroutineScope(Dispatchers.IO)并调用launch方法返回一个Job对象
    主线程在500ms后取消这个协程,但是协程体并不能立即感知自己被取消了,只会执行到withContext才会检测取消信号
  • IO线程上的所有log都会执行,withContext挂起函数之后的代码不再执行

delay挂起函数检测取消信号

kotlin 复制代码
Log.i("yang", "Main Start")
val mJob = CoroutineScope(Dispatchers.IO).launch {
    try {
        Log.i("yang", "IO Start")
        delay(1000)
        Log.i("yang", "IO End")
    } catch (e: Exception) {
        Log.i("yang", "Launch Exception = ${e.message}")
    }
}
Log.i("yang", "Main Continue")
Thread.sleep(500)
mJob.cancel()
Log.i("yang", "Main End")

// log
2025-01-06 16:35:38.555 29643-29643 yang                    com.example.myapplication            I  Main Start
2025-01-06 16:35:38.558 29643-29643 yang                    com.example.myapplication            I  Main Continue
2025-01-06 16:35:38.559 29643-30198 yang                    com.example.myapplication            I  IO Start
2025-01-06 16:35:39.059 29643-29643 yang                    com.example.myapplication            I  Main End
2025-01-06 16:35:39.060 29643-30200 yang                    com.example.myapplication            I  Launch Exception = StandaloneCoroutine was cancelled
  • IO线程创建一个协程作用域CoroutineScope(Dispatchers.IO)并调用launch方法返回一个Job对象
    主线程在500ms后取消这个协程,但是协程体并不能立即感知自己被取消了,只会执行到delay才会检测取消信号
  • IO线程上只有IO Start这一句Log会执行,delay挂起函数后的代码不再执行

CoroutineScope.isActive变量检测取消信号

kotlin 复制代码
Log.i("yang", "Main Start")
 var mJob = CoroutineScope(Dispatchers.IO).launch {
     try {
         Log.i("yang", "IO Start")
         Thread.sleep(1000)
         if (!isActive){
             Log.i("yang", "IO is not active")
             return@launch
         }
         Log.i("yang", "IO End")
     } catch (e: Exception) {
         Log.i("yang", "Launch Exception = ${e.message}")
     }
 }
 Log.i("yang", "Main Continue")
 Thread.sleep(500)
 mJob.cancel()
 Log.i("yang", "Main End")
Log.i("yang", "Main Continue")
Thread.sleep(500)
mJob.cancel()
Log.i("yang", "Main End")

// log
2025-01-06 16:48:17.754 30216-30216 yang                    com.example.myapplication            I  Main Start
2025-01-06 16:48:17.757 30216-30216 yang                    com.example.myapplication            I  Main Continue
2025-01-06 16:48:17.757 30216-30670 yang                    com.example.myapplication            I  IO Start
2025-01-06 16:48:18.257 30216-30216 yang                    com.example.myapplication            I  Main End
2025-01-06 16:48:18.758 30216-30670 yang                    com.example.myapplication            I  IO is not active
  • IO线程创建一个协程作用域CoroutineScope(Dispatchers.IO)并调用launch方法返回一个Job对象
    主线程在500ms后取消这个协程,但是协程体并不能立即感知自己被取消了,只会执行到!isActive才会检测取消信号,提前return
  • IO线程上只有IO Start这一句Log会执行,!isActive后的代码不再执行

CoroutineScope.ensureActive()函数检测取消信号

kotlin 复制代码
Log.i("yang", "Main Start")
var mJob = CoroutineScope(Dispatchers.IO).launch {
    try {
        Log.i("yang", "IO Start")
        Thread.sleep(1000)
        ensureActive()
        Log.i("yang", "IO End")
    } catch (e: Exception) {
        Log.i("yang", "Launch Exception = ${e.message}")
    }
}
Log.i("yang", "Main Continue")
Thread.sleep(500)
mJob.cancel()
Log.i("yang", "Main End")

// log
2025-01-06 17:17:36.781 31763-31763 yang                    com.example.myapplication            I  Main Start
2025-01-06 17:17:36.784 31763-31763 yang                    com.example.myapplication            I  Main Continue
2025-01-06 17:17:36.785 31763-32227 yang                    com.example.myapplication            I  IO Start
2025-01-06 17:17:37.285 31763-31763 yang                    com.example.myapplication            I  Main End
2025-01-06 17:17:37.785 31763-32227 yang                    com.example.myapplication            I  Launch Exception = StandaloneCoroutine was cancelled
  • IO线程创建一个协程作用域CoroutineScope(Dispatchers.IO)并调用launch方法返回一个Job对象
    主线程在500ms后取消这个协程,但是协程体并不能立即感知自己被取消了,只会执行到ensureActive()才会检测取消信号
  • IO线程上只有IO Start这一句Log会执行,ensureActive()后的代码不再执行

取消有返回值的协程任务Deferred

  • 取消有返回值的协程任务Deferred可以复用取消没有返回值的协程任务Job的取消方式
  • 取消整个协程作用域,也会同时取消其中启动的有返回值的协程任务Deferred
相关推荐
小白起 v23 分钟前
三天学完微服务其二
java·微服务·架构
huiyunfei29 分钟前
MinorGC FullGC
java·jvm·算法
Code侠客行39 分钟前
MDX语言的正则表达式
开发语言·后端·golang
编程|诗人40 分钟前
TypeScript语言的正则表达式
开发语言·后端·golang
XWM_Web43 分钟前
JavaAPI.02.包装类与正则表达式
java·开发语言·学习·eclipse
BinaryBardC44 分钟前
R语言的正则表达式
开发语言·后端·golang
CyberScriptor1 小时前
C#语言的字符串处理
开发语言·后端·golang
PangPiLoLo1 小时前
架构学习——互联网常用架构模板
java·学习·微服务·云原生·架构·系统架构·nosql
!!!5251 小时前
SpringBoot-web入门程序剖析
java·spring boot·后端
叶羽西1 小时前
Android GSI (Generic System Image)
android