当前位置:网站首页>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()
}边栏推荐
- Best practices of swagger in egg project
- kotlin 继承、类、重载
- Kubernetes cluster deployment
- 每日一题day8-515. 在每个树行中找最大值
- CVPR 2022 - Interpretation of selected papers of meituan technical team
- Cloud native essay solicitation progress case practice
- Prometheus PushGateway 碎碎念
- Redis scenario
- Beauty of script │ VBS introduction interactive practice
- Understanding openstack network
猜你喜欢

快速了解常用的消息摘要算法,再也不用担心面试官的刨根问底

首席信息安全官仍然会犯的漏洞管理错误

Hardware development notes (6): basic process of hardware development, making a USB to RS232 module (5): creating USB package library and associating principle graphic devices

这几个默认路由、静态路由的配置部署都不会,还算什么网工!

脚本之美│VBS 入门交互实战

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

万用表的使用方法

DTU上报的数据值无法通过腾讯云规则引擎填入腾讯云数据库中

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

These default routes and static routes can not be configured and deployed. What kind of network workers are they!
随机推荐
万用表的使用方法
Developer survey: rust/postgresql is the most popular, and PHP salary is low
How can junior middle school developers effectively reduce their own workload?
首席信息安全官仍然会犯的漏洞管理错误
I have fundamentally solved the problem of wechat occupying mobile memory
Use of kotlin arrays, collections, and maps
Why does the kubernetes environment require that bridge NF call iptables be enabled?
CPU status information us, sy and other meanings
服务可见可观测性
国内首款开源MySQL HTAP数据库即将发布,三大看点提前告知
kotlin 关键字 扩展函数
Yolov6: the fast and accurate target detection framework is open source
Resolve symbol conflicts for dynamic libraries
源碼解析 Handler 面試寶典
Nifi from introduction to practice (nanny level tutorial) - environment
Huawei PC grows against the trend, and product power determines everything
谁是鱼谁是饵?红队视角下蜜罐识别方式汇总
8 - Format integers and floating point numbers
Integrate API interface parameter Dictionary of accounts of multiple local distribution companies - Express 100
Activity生命周期