当前位置:网站首页>kotlin 组合挂起函数
kotlin 组合挂起函数
2022-06-24 12:53:00 【day_moon】
//1.按顺序执行 fun main1() = runBlocking<Unit> { val time = measureTimeMillis { //计算时间 var one = doOne() var two = doTwo() println("onetwo Time is ${one+two}")//相加 } println("Time is ${time}") } suspend fun doOne(): Int { delay(1000)//延迟1秒 return 1 } suspend fun doTwo(): Int { delay(2000)//延迟2秒 return 2 } //2.并发执行 async fun mainAsync() = runBlocking<Unit> { val time = measureTimeMillis { //计算时间 var one = async { doOne()}// async 启动一个单独的协程 返回一个轻量级非阻塞的 Deferred 对象 var two = async { doTwo()}// println("onetwo Time is ${one.await()+two.await()}")//one.await()获取返回的值 } println("Time is ${time}") } //3.惰性启动 fun mainLazy() = runBlocking<Unit> { val time = measureTimeMillis { //计算时间 var one = async(start = CoroutineStart.LAZY) { doOne()}// async 启动一个单独的协程 返回一个轻量级非阻塞的 Deferred 对象 var two = async (start = CoroutineStart.LAZY){ doTwo()} one.start()//开始执行协程 ,注意如果 先调用await() 会启动协程并等待其完成 two.start() println("onetwo Time is ${one.await()+two.await()}")//one.await()获取返回的值 } println("Time is ${time}") } //4.并发结构 fun main_async1() = runBlocking<Unit> { val time = measureTimeMillis { //计算时间 doThings() } println("Time is ${time}") } suspend fun doThings():Int=coroutineScope { var one = async() { doOnes() }// async 启动一个单独的协程 返回一个轻量级非阻塞的 Deferred 对象 var two = async() { doTwos() } println("onetwo Time is ${one.await()+two.await()}")//one.await()获取返回的值 one.await() + two.await()//one.await()获取返回的值 } 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()//先子类协程抛异常 然后父类协程再抛异常 } one.await()+two.await() }
边栏推荐
- Kotlin language features
- The agile way? Is agile development really out of date?
- Google Earth engine - Murray global tidal wetland change V1 data set from 1999 to 2019
- Kotlin inheritance, class, overload
- What is the difference between sap QM and UD for inspection lots with hum?
- Android kotlin Encyclopedia
- Kotlin initialization block
- 硬件开发笔记(六): 硬件开发基本流程,制作一个USB转RS232的模块(五):创建USB封装库并关联原理图元器件
- [AI player cultivation record] use AI to identify what kind of wealth is next door
- 图扑软件数字孪生海上风电 | 向海图强,奋楫争先
猜你喜欢
随机推荐
Can inspection results be entered after the completion of inspection lot UD with long-term inspection characteristics in SAP QM?
SYSTEMd common component description
华为 PC 逆势增长,产品力决定一切
Android kotlin 大全
[one picture series] one picture to understand Tencent Qianfan ipaas
Troubleshooting the kubernetes problem: deleting the rancher's namespace by mistake causes the node to be emptied
The agile way? Is agile development really out of date?
金鱼哥RHCA回忆录:DO447管理清单和凭据--为访问清单主机创建机器凭据
Use abp Zero builds a third-party login module (I): Principles
Introduction to reptile to give up 01: Hello, reptile!
Developer survey: rust/postgresql is the most popular, and PHP salary is low
openGauss内核:简单查询的执行
kotlin 初始化块
CVPR 2022 | 美团技术团队精选论文解读
Vipshop's "special sale" business is no longer easy to do?
【AI玩家养成记】用AI识别邻居家旺财是什么品种
Kotlin initialization block
青藤入选工信部网安中心“2021年数字技术融合创新应用典型解决方案”
Yyds dry goods counting solution sword finger offer: adjust the array order so that odd numbers precede even numbers (2)
What is the difference between sap QM and UD for inspection lots with hum?