当前位置:网站首页>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()
}
}
}
边栏推荐
- 去除网页滚动条方法以及内外边距
- Network security - Information Collection
- 疫情当头,作为Leader如何进行团队的管理?| 社区征文
- 单词单词单词
- [leetcode] 797 and 1189 (basis of graph theory)
- 网络安全-ACL访问控制列表
- Redis:Redis的简单使用
- 浏览器是如何对页面进行渲染的呢?
- Method of removing webpage scroll bar and inner and outer margins
- Huakaiyun (Zhiyin) | virtual host: what is a virtual host
猜你喜欢
[fluent] hero animation (hero animation use process | create hero animation core components | create source page | create destination page | page Jump)
Stm32f407 ------- IIC communication protocol
elastic stack
Processing of tree structure data
Rockchip3399 start auto load driver
Query product cases - page rendering data
[camera topic] complete analysis of camera dtsi
详细些介绍如何通过MQTT协议和华为云物联网进行通信
可視化yolov5格式數據集(labelme json文件)
Hard core observation 547 large neural network may be beginning to become aware?
随机推荐
[Appendix 6 Application of reflection] Application of reflection: dynamic agent
Comment le chef de file gère - t - il l'équipe en cas d'épidémie? Contributions communautaires
Iptables layer 4 forwarding
How to find summer technical internship in junior year? Are you looking for a large company or a small company for technical internship?
In 2022, 95% of the three most common misunderstandings in software testing were recruited. Are you that 5%?
疫情当头,作为Leader如何进行团队的管理?| 社区征文
[leetcode] 797 and 1189 (basis of graph theory)
【Camera专题】OTP数据如何保存在自定义节点中
使用Go语言实现try{}catch{}finally
Network security - password cracking
Network security - cracking system passwords
Trial setup and use of idea GoLand development tool
MySQL learning 03
Depth (penetration) selector:: v-deep/deep/ and > > >
PS remove watermark details
浏览器是如何对页面进行渲染的呢?
Network security - talking about security threats
Network security - scanning and password explosion 2
DQL basic operation
When the epidemic comes, how to manage the team as a leader| Community essay solicitation