当前位置:网站首页>Kotlin starts the process, the difference between launch and async, and starts the process in sequence
Kotlin starts the process, the difference between launch and async, and starts the process in sequence
2022-06-12 03:40:00 【Ango cannot move】
launch And async The difference between
binding.btClick5.setOnClickListener {
// The main thread executes the coroutine
runBlocking {
//launch The execution result will not be returned
val job1 = launch {
delay(200)
Log.e(TAG, "onCreate: jbo1 finished.")
}
// async The execution result will be returned
val job2 = async {
delay(200)
Log.e(TAG, "onCreate: job2 finished.")
"job2 result"
}
Log.e(TAG, "onCreate${ job2.await() }")
}
}Start the process in sequence
Now there are three processes . Want to start first 1
Then start up 2 and 3
How to operate ,
luanch Start up Use join function
binding.btClick5.setOnClickListener {
// The main thread executes the coroutine
runBlocking {
//launch The execution result will not be returned
val job1 = launch {
delay(2000)
Log.e(TAG, "One")
}
job1.join()
val job2 = launch {
delay(200)
Log.e(TAG, "Two")
}
val job3 = launch {
delay(200)
Log.e(TAG, "Three")
}
}
}async Words Use await
binding.btClick5.setOnClickListener {
// The main thread executes the coroutine
runBlocking {
//launch The execution result will not be returned
val job1 = async {
delay(2000)
Log.e(TAG, "One")
}
job1.await()
val job2 = async {
delay(200)
Log.e(TAG, "Two")
}
val job3 = async {
delay(200)
Log.e(TAG, "Three")
}
}
}
join and await Are all suspended functions , Will not block the main thread
边栏推荐
- 数据库精选 60 道面试题
- Convert py file to EXE file
- vim命令大全
- 2022 communication industry ultimate Exhibition Guide
- 分布式计算的八大谬论
- Final summary of addition, deletion, modification and query - 2.2 (knowledge points in mapper.xml)
- Yyds dry inventory MySQL learning - how transactions are isolated
- (idea)the file size(2.85M) exceeds configured limit(2.56M).Code insight features are not available问题
- Simple database connection example
- tcp 三次握手与四次挥手
猜你喜欢

无限循环判断方法;

数据库精选 60 道面试题
![[Business Research Report] forward looking report on urban renewal and development in China in 2021 - download link attached](/img/5d/15615ad26dc3dbb10b0b9de8813848.jpg)
[Business Research Report] forward looking report on urban renewal and development in China in 2021 - download link attached

Detailed explanation of data platform SQL development and function use

AI interview bag | Netease mutual entertainment AI Lab artificial intelligence research engineers share on both sides

Sequence list and linked list - primary level

Paper recommendation: relicv2, can the new self supervised learning surpass supervised learning on RESNET?

Mongodb essence summary

How to modify the result name of MySQL query result 1 and result 2

Kubernetes affinity learning notes
随机推荐
简单的数据库连接示例
[DFS "want" or "don't"] seek subsets; Seeking combination
大整数的加与乘;
技术经济与企业管理 复习 第四章
R语言plotly可视化:可视化回归模型实际值和回归预测值的散点图分析回归模型的预测效能、区分训练集和测试集、一个好的模型大部分的散点在对角线附近、添加边缘直方图以快速诊断模型可能存在的任何预测偏差
Pat class B 1067 trial password (20 points)
Message queuing overview
mysql/oracle 以唯一时间为分界,小于等于该时间求和,大于该时间求和
Concept and introduction of microservice
[Bank Research Report] technology enabled retail finance carbon neutral development report (2022) - download link attached
tcp 三次握手与四次挥手
What is a request response pair called? [repeat] - what is a request response pair called? [duplicate]
postgresql基本介绍以及部署使用
MySQL的check约束数字问题
双目标定学习资料整理
绝对定位使盒子居中显示的三种方式
Data flow diagram of Flink
Inverted string - two solutions
数据库精选 60 道面试题
R语言plotly可视化:使用plotly可视化简单线性回归模型的回归线(simple regression model linear regression plots)