当前位置:网站首页>Kotlin composite suspend function
Kotlin composite suspend function
2022-06-24 13:39:00 【day_ moon】
//1. Execute in order
fun main1() = runBlocking<Unit> {
val time = measureTimeMillis { // computing time
var one = doOne()
var two = doTwo()
println("onetwo Time is ${one+two}")// Add up
}
println("Time is ${time}")
}
suspend fun doOne(): Int {
delay(1000)// Delay 1 second
return 1
}
suspend fun doTwo(): Int {
delay(2000)// Delay 2 second
return 2
}
//2. Concurrent execution async
fun mainAsync() = runBlocking<Unit> {
val time = measureTimeMillis { // computing time
var one = async { doOne()}// async Start a separate collaboration Returns a lightweight nonblocking Deferred object
var two = async { doTwo()}//
println("onetwo Time is ${one.await()+two.await()}")//one.await() Get the returned value
}
println("Time is ${time}")
}
//3. Inert start
fun mainLazy() = runBlocking<Unit> {
val time = measureTimeMillis { // computing time
var one = async(start = CoroutineStart.LAZY) { doOne()}// async Start a separate collaboration Returns a lightweight nonblocking Deferred object
var two = async (start = CoroutineStart.LAZY){ doTwo()}
one.start()// Start execution of collaboration , Note that if First call await() Will start the process and wait for it to complete
two.start()
println("onetwo Time is ${one.await()+two.await()}")//one.await() Get the returned value
}
println("Time is ${time}")
}
//4. Concurrent structure
fun main_async1() = runBlocking<Unit> {
val time = measureTimeMillis { // computing time
doThings()
}
println("Time is ${time}")
}
suspend fun doThings():Int=coroutineScope {
var one = async() { doOnes() }// async Start a separate collaboration Returns a lightweight nonblocking Deferred object
var two = async() { doTwos() }
println("onetwo Time is ${one.await()+two.await()}")//one.await() Get the returned value
one.await() + two.await()//one.await() Get the returned value
}
suspend fun doOnes(): Int {
delay(1000) // pretend we are doing something useful here
return 1
}
suspend fun doTwos(): Int {
delay(2000) // pretend we are doing something useful here
return 2
}
fun main_async2() = runBlocking<Unit> {
try {
currentSum()
}catch (e:Exception){
println("main_async2 Exception..")
}
}
suspend fun currentSum():Int= coroutineScope {
val one=async<Int> {
try {
delay(1000)
}finally {
println("one finally")
}
1
}
val two=async<Int> {
delay(2000)
println("two exception..")
throw ArithmeticException()// Pre subclass co process throwing exception Then the parent class coroutine throws an exception
}
one.await()+two.await()
}边栏推荐
- Kubernetes cluster deployment
- Main steps of system test
- 谁是鱼谁是饵?红队视角下蜜罐识别方式汇总
- SYSTEMd common component description
- Integrate the authorization interface code of intra city distribution account of multiple express companies nationwide - Express 100
- CPU process priority
- Internet of things? Come and see Arduino on the cloud
- 国内首款开源MySQL HTAP数据库即将发布,三大看点提前告知
- System status identifier 'hum' for SAP QM inspection lot
- 手机开户后多久才能通过?在线开户安全么?
猜你喜欢

Seven challenges faced by data scientists and Solutions

#云原生征文#Ingress案例实战

常识知识点

The data value reported by DTU cannot be filled into Tencent cloud database through Tencent cloud rule engine

Who is the fish and who is the bait? Summary of honeypot recognition methods from the perspective of red team

Getting started with the go Cobra command line tool

These default routes and static routes can not be configured and deployed. What kind of network workers are they!

国内首款开源MySQL HTAP数据库即将发布,三大看点提前告知

1. Snake game design

Comparator 排序函数式接口
随机推荐
不用Home Assistant,智汀也开源接入HomeKit、绿米设备?
Understanding openstack network
黄楚平主持召开定点联系珠海工作视频会议 坚决落实省委部署要求 确保防疫情、稳经济、保安全取得积极成效
Vim 常用快捷键
Why does the kubernetes environment require that bridge NF call iptables be enabled?
Activity生命周期
CVPR 2022 - Interpretation of selected papers of meituan technical team
金鱼哥RHCA回忆录:DO447管理项目和开展作业--为ansible剧本创建一个项目
问个sql view的问题
Huawei PC grows against the trend, and product power determines everything
Cloud native essay solicitation progress case practice
快速了解常用的消息摘要算法,再也不用担心面试官的刨根问底
服务可见可观测性
Comparator sort functional interface
39 - read XML node and attribute values
Integrated API interface code of domestic express companies for intra city distribution and ordering - Express 100
kotlin 关键字 扩展函数
Geological disaster early warning monitoring RTU
[log service CLS] Tencent cloud log service CLS accesses CDN
kotlin 接口 泛型 协变 逆变