当前位置:网站首页>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()
}
}
}
边栏推荐
- 2022 financial product revenue ranking
- 我的创作纪念日
- easyPOI
- CFdiv2-Fixed Point Guessing-(區間答案二分)
- Summary of ES6 filter() array filtering methods
- [shutter] shutter debugging (debugging control related functions | breakpoint management | code operation control)
- Network security - vulnerabilities and Trojans
- Certaines fonctionnalités du développement d'applets
- What are MySQL locks and classifications
- [AUTOSAR cantp] -2.11-uds diagnostic response frame data segment data padding data filling and data optimization data optimization (Theory + configuration)
猜你喜欢

Ni visa fails after LabVIEW installs the third-party visa software

MySQL学习03

Trial setup and use of idea GoLand development tool

easyPOI
![[camera topic] complete analysis of camera dtsi](/img/cb/d42589fcf0610600c9dc8c7992d4d7.png)
[camera topic] complete analysis of camera dtsi

The testing process that software testers should know

easyExcel

可視化yolov5格式數據集(labelme json文件)

How to deal with cache hot key in redis

Anna: Beibei, can you draw?
随机推荐
Explore the conversion between PX pixels and Pt pounds, mm and MM
How do it students find short-term internships? Which is better, short-term internship or long-term internship?
Solution for processing overtime orders (Overtime unpaid)
[camera special topic] Hal layer - brief analysis of addchannel and startchannel
In 2022, 95% of the three most common misunderstandings in software testing were recruited. Are you that 5%?
How can retail enterprises open the second growth curve under the full link digital transformation
Swift开发学习
各国Web3现状与未来
Stm32f407 ------- IIC communication protocol
Learn BeanShell before you dare to say you know JMeter
y54.第三章 Kubernetes从入门到精通 -- ingress(二七)
Network security - vulnerabilities and Trojans
Processing of tree structure data
Performance test | script template sorting, tool sorting and result analysis
MySQL learning 03
《上市风云》荐书——唯勇气最可贵
Rockchip3399 start auto load driver
网络安全-ACL访问控制列表
[shutter] pull the navigation bar sideways (drawer component | pageview component)
Hard core observation 547 large neural network may be beginning to become aware?