当前位置:网站首页>Four startup modes of kotlin collaboration
Four startup modes of kotlin collaboration
2022-06-13 00:46:00 【Ango cannot move】
Default : Start scheduling immediately after the collaboration is created . If the collaboration is cancelled before scheduling . Put it directly into the corresponding cancellation state .
ATOMIC: After the collaboration is created . Start scheduling immediately . The collaboration process will not be canceled until the first starting point .
LAZY: Only when collaboration is needed , Including active coordination process start、join perhaps await Wait for the function , Will start scheduling , If it is cancelled before scheduling . Then the collaboration will directly enter the abnormal state .
UNDISPATCHED: The coroutine is executed in the current function call stack immediately after it is created , Until you meet the first real hang point .
CoroutineStart.DEFAULT
Code case
binding.btClick7.setOnClickListener {
runBlocking {
val job = launch(start = CoroutineStart.DEFAULT) {
delay(10000)
Log.e(TAG, "onCreate: Job finished.")
}
delay(1000)
job.cancel()
Log.e(TAG, "onCreate: Job cancel.")
}
}
The coordination process will not execute
Because it was cancelled during the execution .
ATOMIC
binding.btClick7.setOnClickListener {
runBlocking {
val job = launch(start = CoroutineStart.ATOMIC) {
// Here code
// These tasks must be performed
// Here's the code
// Here is a starting point
delay(10000)
Log.e(TAG, "onCreate: Job finished.")
}
delay(1000)
job.cancel()
Log.e(TAG, "onCreate: Job cancel.")
}
}
The code before the function is suspended will not be cancelled . Cancellation can only be carried out after the starting point is reached
LAZY
runBlocking {
val job = async(start = CoroutineStart.LAZY) {
29
}
// Perform calculations
// Start the coroutines
Log.e(TAG, "Completed job.start in ${job.start()} ms")
// Or use await
job.cancel()
job.await()
job.join()
}
job.cancel() Will enter an abnormal state . If not await、join、 Or is it start operation Will not be activated .
By default
val job = async(context = Dispatchers.IO, start = CoroutineStart.DEFAULT) {
Log.e(TAG, "thread:" + Thread.currentThread().name)
}
Will be executed in the child thread
If you use
UNDISPATCHED Pattern
binding.btClick7.setOnClickListener {
runBlocking {
val job = async(context = Dispatchers.IO, start = CoroutineStart.UNDISPATCHED) {
Log.e(TAG, "thread:" + Thread.currentThread().name)
}
}
}
Will print out the main thread
The printing at the bottom corresponds to the above default mode
Why does it print out the main thread
The coroutine is executed in the current function call stack immediately after it is created , Because it is executed in the main thread . So it will execute in the main thread .
UNDISPATCHED It means no forwarding .
边栏推荐
- Oceanbase is the leader in the magic quadrant of China's database in 2021
- Arduino controls tb6600 driver +42 stepper motor
- Easyexcel read excel simple demo
- After so long use, CSDN has finally opened a blog
- Use of split() method in string class
- pytorch是什么?解释pytorch的基本概念
- 6.824 Lab 2: Raft
- Android Weather
- Paper reading and sharing
- Comparison of disk partition modes (MBR and GPT)
猜你喜欢
Kalix system - use of information collection gadgets
Build your own PE manually from winpe of ADK
Kotlin 协程的四种启动模式
Programming training 1
How to solve the duplication problem when MySQL inserts data in batches?
[CISCN2019 华北赛区 Day2 Web1]Hack World --BUUCTF
BUUCTF之BabyUpload[GXYCTF2019]
Installation of IK word breaker
[MRCTF2020]Ez_ bypass --BUUCTF
Successfully installed opencv under delphixe
随机推荐
Aunt learning code sequel: ability to sling a large number of programmers
Android Weather
Successfully installed opencv under delphixe
[imx6ull] video monitoring project (USB camera +ffmepeg)
Arduino control soil moisture sensor
Canvas random bubbling background
Maybe we can figure out the essence of the Internet after the dust falls
OceanBase 雄踞墨天轮2021年度中国数据库魔力象限领导者
DNS attack surface analysis
[sca-cnn interpretation] spatial and channel wise attention
Map from getting started to performance optimization
[MRCTF2020]Ez_bypass --BUUCTF
JPA execution failed in scheduled task -executing an update/delete query transactionrequiredexception
为什么磁盘分区的时候,第一个分区前面总有一段空间(63或者2048个扇区)
Canvas airplane game
Using fastjson to solve the problem of returning an empty array from a null value of a field string object
Kotlin 协程的作用域构建器 coroutineScope与runBlocking 与supervisorScope,协程同步运行,协程挂掉的时候其他协程如何不被挂掉。
Through MDC, you can easily track your scheduled task log
kotlin 协程withContext切换线程
RCC clock configuration of stm32f401