当前位置:网站首页>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 .
边栏推荐
- Canvas airplane game
- Set sail
- 杂记:intel11代和12代移动版支持原生Thunderbolt4接口,桌面版不支持
- Card constructions -- two points
- Oceanbase is the leader in the magic quadrant of China's database in 2021
- New blog address
- MySQL query table field information
- 6.824 Lab 4B: Sharded Key/Value Service
- Why is there always a space (63 or 2048 sectors) in front of the first partition when partitioning a disk
- The origin of MySQL in bedtime stories
猜你喜欢
![[ciscn2019 North China Day2 web1]hack world --buuctf](/img/1e/ebf8cc06ba620261f03f84ca2c68e6.png)
[ciscn2019 North China Day2 web1]hack world --buuctf
![[GXYCTF2019]禁止套娃--详解](/img/c8/8c588ab8f58e2b38b9c64c4ccd733f.png)
[GXYCTF2019]禁止套娃--详解

MySQL locates the position of the character in the string String substitution

Arduino controls tb6600 driver +42 stepper motor

Kotlin 协程,job的生命周期

深度学习训练多少轮?迭代多少次?

磁盘分区方式对比(MBR与GPT)

antdPro - ProTable 实现两个选择框联动效果

Kotlin 协程挂起函数 suspend 关键字

What is meebits? A brief explanation
随机推荐
[JS] solve the problem that removeeventlistener is invalid after the class listening event from new is bound to this
Using com0com/com2tcp to realize TCP to serial port (win10)
MySQL query table field information
高阶极点对于波形的影响
gpu加速pytorch能用吗?
[sca-cnn interpretation] spatial and channel wise attention
Stack overflow learning summary
Kali system -- dnsmap for DNS collection and analysis
ROS2之OpenCV人脸识别foxy~galactic~humble
Kotlin 协程的四种启动模式
Andersen Global通过在芬兰和丹麦的合作协议拓展北欧地区业务版图
6.824 Lab 1: MapReduce
Kotlin 协程挂起函数 suspend 关键字
【SCA-CNN 解读】空间与通道注意力:Spatial and Channel-wise Attention
Paper reading and sharing
Successfully installed opencv under delphixe
什么是 Meebits?一个简短的解释
Androi天气
[imx6ull] video monitoring project (USB camera +ffmepeg)
也许尘埃落地,我们才能想清楚互联网的本质