当前位置:网站首页>Startup mode and scope builder of collaboration in kotlin
Startup mode and scope builder of collaboration in kotlin
2022-07-03 02:07:00 【yu-Knight】
Kotlin Startup mode and scope builder of middle process
The startup mode of the coroutine
1.DEFAULT: After the collaboration is created , Start scheduling immediately , If the collaboration is cancelled before scheduling , It will go directly to the state of canceling the response .
2.ATOMIC: After the collaboration is created , Start scheduling immediately , The coroutine does not respond to cancellation until it reaches the first hang point
3.LAZY: Only when collaboration is needed , Including the method of actively calling the coroutine start、join perhaps await Wait for the function to start scheduling , If it is cancelled before scheduling , Then the collaboration will directly enter the abnormal end state .
4.UNDISPATCHED: The coroutine is executed in the current function call stack immediately after it is created , Until you meet the first real hang point .
@Test
fun `test start mode`() = runBlocking {
/* val job1 = launch(start = CoroutineStart.DEFAULT) { delay(10000) println("Job finished.") } delay(1000) job1.cancel() val job2 = launch(start = CoroutineStart.ATOMIC) { //... This paragraph must be implemented delay(10000) // Here is the first starting point println("Job finished.") } delay(1000) job2.cancel() val job3 = async(start = CoroutineStart.LAZY) { 29 } //... Calculation job3.await() */
// Current function call stack main In the main thread
val job4 = async(context = Dispatchers.IO, start = CoroutineStart.UNDISPATCHED) {
println("thread:" + Thread.currentThread().name)
}
}
Scope builder of the collaboration
coroutineScope And runBlocking
1.runBlocking It's a regular function , and coroutineScope It's a suspend function .
2. They all wait for the end of their coroutine body and all child coroutines , The main difference is that runBlocking Method blocks the current thread to wait , and coroutineScope Only hang , The underlying thread is freed for other purposes .
coroutineScope And supervisorScope
1.coroutineScope: A collaboration failed , All other brotherhood programs will also be cancelled .
2.supervisorScope: A collaboration failed , It won't affect other brothers' cooperation .
@Test
fun `test coroutine scope builder`() = runBlocking {
coroutineScope {
//job1 Zixie Cheng
val job1 = launch {
delay(400)
println("job1 finished.")
}
//job2 Zixie Cheng
val job2 = async {
delay(200)
println("job2 finished.")
"job2 result" //async Return value
//job2 Exception thrown in ,job1 and job2 Will be canceled
throw IllegalArgumentException()
}
}
}
@Test
fun `test supervisor scope builder`() = runBlocking {
supervisorScope {
//job1 Zixie Cheng
val job1 = launch {
delay(400)
println("job1 finished.")
}
//job2 Zixie Cheng
val job2 = async {
delay(200)
println("job2 finished.")
"job2 result" //async Return value
//job2 Exception thrown in , Does not affect the job1
throw IllegalArgumentException()
}
}
}
边栏推荐
- In the face of difficult SQL requirements, HQL is not afraid
- [Yu Yue education] reference materials of love psychology of China University of mining and technology
- Cfdiv2 fixed point guessing- (interval answer two points)
- Visualisation de l'ensemble de données au format yolov5 (fichier labelme json)
- Network security - vulnerabilities and Trojans
- es6 filter() 数组过滤方法总结
- 各国Web3现状与未来
- Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance o
- Depth (penetration) selector:: v-deep/deep/ and > > >
- [Yu Yue education] Jiujiang University material analysis and testing technology reference
猜你喜欢
LabVIEW安装第三方VISA软件后NI VISA失效
机器学习笔记(持续更新中。。。)
MySQL学习03
Introduce in detail how to communicate with Huawei cloud IOT through mqtt protocol
Processing of tree structure data
[Appendix 6 Application of reflection] Application of reflection: dynamic agent
深度学习笔记(持续更新中。。。)
Rockchip3399 start auto load driver
ByteDance data Lake integration practice based on Hudi
easyPOI
随机推荐
[shutter] shutter debugging (debugging fallback function | debug method of viewing variables in debugging | console information)
[Yu Yue education] Jiujiang University material analysis and testing technology reference
微信小程序開發工具 POST net::ERR_PROXY_CONNECTION_FAILED 代理問題
Rockchip3399 start auto load driver
Button button adaptive size of wechat applet
Network security - vulnerabilities and Trojans
小程序開發的部分功能
Some functions of applet development
The testing process that software testers should know
Network security - the simplest virus
【Camera专题】HAL层-addChannel和startChannel简析
Answers to ten questions about automated testing software testers must see
詳細些介紹如何通過MQTT協議和華為雲物聯網進行通信
使用Go语言实现try{}catch{}finally
树形结构数据的处理
Query product cases - page rendering data
Socket编程
502 (bad gateway) causes and Solutions
stm32F407-------ADC
Network security - scan