当前位置:网站首页>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()
}
}
}
边栏推荐
- How do browsers render pages?
- Some functions of applet development
- 技术大佬准备就绪,话题C位由你决定
- Analysis, use and extension of open source API gateway apisex
- easyExcel
- 2022 financial product revenue ranking
- Network security - dynamic routing protocol rip
- 可視化yolov5格式數據集(labelme json文件)
- 疫情當頭,作為Leader如何進行團隊的管理?| 社區征文
- 【Camera专题】Camera dtsi 完全解析
猜你喜欢

MySQL learning 03

What are the key points often asked in the redis interview
![[error record] an error is reported in the fluent interface (no mediaquery widget ancestor found. | scaffold widgets require a mediaquery)](/img/fd/d862412db43a641537fd67f7910741.jpg)
[error record] an error is reported in the fluent interface (no mediaquery widget ancestor found. | scaffold widgets require a mediaquery)
![[shutter] shutter debugging (debugging fallback function | debug method of viewing variables in debugging | console information)](/img/66/0fda43da0d36fc0c9277ca86ece252.jpg)
[shutter] shutter debugging (debugging fallback function | debug method of viewing variables in debugging | console information)
![[fluent] fluent debugging (debug debugging window | viewing mobile phone log information | setting normal breakpoints | setting expression breakpoints)](/img/ac/bf83f319ea787c5abd7ac3fabc9ede.jpg)
[fluent] fluent debugging (debug debugging window | viewing mobile phone log information | setting normal breakpoints | setting expression breakpoints)

全链路数字化转型下,零售企业如何打开第二增长曲线

小程序開發的部分功能

stm32F407-------ADC

《上市风云》荐书——唯勇气最可贵
![[camera topic] turn a drive to light up the camera](/img/d3/7aabaa5c75813abc4a43820b4c3706.png)
[camera topic] turn a drive to light up the camera
随机推荐
Return the only different value (de duplication)
我的创作纪念日
Network security - scanning and password explosion 2
Network security - man in the middle attack
His experience in choosing a startup company or a big Internet company may give you some inspiration
缺少库while loading shared libraries: libisl.so.15: cannot open shared object file: No such file
DML Foundation
[leetcode] 797 and 1189 (basis of graph theory)
机器学习流程与方法
2022 financial product revenue ranking
Cfdiv2 fixed point guessing- (interval answer two points)
Bottleneck period must see: how can testers who have worked for 3-5 years avoid detours and break through smoothly
stm32F407-------DMA
Network security - phishing
Swift开发学习
Learn BeanShell before you dare to say you know JMeter
Depth (penetration) selector:: v-deep/deep/ and > > >
Network security - firewall
[fluent] fluent debugging (debug debugging window | viewing mobile phone log information | setting normal breakpoints | setting expression breakpoints)
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance o