当前位置:网站首页>The scope builder coroutinescope, runblocking and supervisorscope of kotlin collaboration processes run synchronously. How can other collaboration processes not be suspended when the collaboration pro
The scope builder coroutinescope, runblocking and supervisorscope of kotlin collaboration processes run synchronously. How can other collaboration processes not be suspended when the collaboration pro
2022-06-13 00:46:00 【Ango cannot move】
coroutineScope And runBlocking difference
runBlocking It's a regular function , and coroutineScope It's a suspend function
They will all wait for the end of their process body and all sub processes , The main difference is that runBolocking Method blocks the current thread to wait . and coroutineScope Just hang up , The underlying thread is freed for other purposes .
runBlocking Code example
binding.btClick7.setOnClickListener {
runBlocking {
val job1 = async {
delay(1000)
Log.e(TAG, "job1 finished")
throw IllegalArgumentException()
}
val job2 = async {
delay(2000)
Log.e(TAG, "job2 finished")
}
}
Log.e(TAG, "thread:A")
}
app Will flash back
Then change to coroutineScope
runBlocking {
coroutineScope {
val job1 = async {
delay(1000)
Log.e(TAG, "job1 finished")
throw IllegalArgumentException()
}
val job2 = async {
delay(2000)
Log.e(TAG, "job2 finished")
}
}
}
Log.e(TAG, "thread:A")
} 
app It won't go back . The following will not be executed .
If you want to report an error, the rest will not be affected , Use supervisorScope , How to operate
binding.btClick7.setOnClickListener {
runBlocking {
supervisorScope {
val job1 = async {
delay(1000)
Log.e(TAG, "job1 finished")
throw IllegalArgumentException()
}
val job2 = async {
delay(2000)
Log.e(TAG, "job2 finished")
}
}
}
Log.e(TAG, "thread:A")
}
coroutineScope A collaboration failed . All other collaborations will be cancelled
supervisorScope A collaboration failed . It won't affect other brotherhood .
边栏推荐
- Aunt learning code sequel: ability to sling a large number of programmers
- Installation of IK word breaker
- [network protocol] problems and solutions in the use of LwIP
- Arduino uses esp8266+ lighting technology + Xiaoai audio to realize voice control switch
- New blog address
- What is meebits? A brief explanation
- Map from getting started to performance optimization
- BUUCTF之BabySQL[极客大挑战 2019]
- gpu加速pytorch能用吗?
- Penetration test summary
猜你喜欢

AOF持久化

Hard (magnetic) disk (I)
![[Error] invalid use of incomplete type 使用了未定义的类型](/img/8a/7cb5d270cfd8831ddc146687fe4499.png)
[Error] invalid use of incomplete type 使用了未定义的类型

MySQL queries the quantity of each month and the year-on-year and month on month data of each month
![[MRCTF2020]Ez_ bypass --BUUCTF](/img/73/85262c048e177968be67456fa4fe02.png)
[MRCTF2020]Ez_ bypass --BUUCTF
![[GXYCTF2019]禁止套娃--详解](/img/c8/8c588ab8f58e2b38b9c64c4ccd733f.png)
[GXYCTF2019]禁止套娃--详解

今日睡眠质量记录74分

Win10 home vs pro vs enterprise vs enterprise LTSC
![[MRCTF2020]Ez_bypass --BUUCTF](/img/73/85262c048e177968be67456fa4fe02.png)
[MRCTF2020]Ez_bypass --BUUCTF

People and gods are angry. Details of Tangshan "mass beating of women incident"
随机推荐
Kali system -- dnsmap for DNS collection and analysis
What is pytorch? Explain the basic concepts of pytorch
MySQL query table field information
Introduction to ROS from introduction to mastery (zero) tutorial
什么是 Meebits?一个简短的解释
Aunt learning code sequel: ability to sling a large number of programmers
DNS attack surface analysis
Development notes of Mongoose
RCC clock configuration of stm32f401
gpu加速pytorch能用吗?
Kotlin 协程的作用域构建器 coroutineScope与runBlocking 与supervisorScope,协程同步运行,协程挂掉的时候其他协程如何不被挂掉。
Stm32f4 development of DMA transmission to GPIO port
Delphi Chinese digit to Arabic digit
Using com0com/com2tcp to realize TCP to serial port (win10)
Set sail
[virtual machine] notes on virtual machine environment problems
Converting Chinese numbers to Arabic numbers in Delphi
[imx6ull] video monitoring project (USB camera +ffmepeg)
Hard (magnetic) disk (II)
Canvas game lower level 100