当前位置:网站首页>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() }
边栏推荐
- kotlin 接口 泛型 协变 逆变
- 手机开户后多久才能通过?在线开户安全么?
- Vipshop's "special sale" business is no longer easy to do?
- kotlin 协程 lanch 详解
- Golden age ticket: Web3.0 Security Manual
- How stupid of me to hire a bunch of programmers who can only "Google"!
- 开发者调查:Rust/PostgreSQL 最受喜爱,PHP 薪水偏低
- 工业物联网(IIoT)的八个主要趋势
- 我从根上解决了微信占用手机内存问题
- Kotlin inheritance, class, overload
猜你喜欢
‘高并发&高性能&高可用服务程序’编写及运维指南
Creation and use of unified links in Huawei applinking
Vulnerability management mistakes that CIOs still make
Cloud native essay solicitation progress case practice
万用表的使用方法
快速了解常用的消息摘要算法,再也不用担心面试官的刨根问底
谁是鱼谁是饵?红队视角下蜜罐识别方式汇总
Interviewer: the MySQL database is slow to query. What are the possible reasons besides the index problem?
These default routes and static routes can not be configured and deployed. What kind of network workers are they!
Party, Google's autoregressive Wensheng graph model
随机推荐
On the difference between process and thread
Richard Sutton, the father of reinforcement learning, paper: pursuing a general model for intelligent decision makers
hands-on-data-analysis 第三单元 模型搭建和评估
黄楚平主持召开定点联系珠海工作视频会议 坚决落实省委部署要求 确保防疫情、稳经济、保安全取得积极成效
Getting started with the lvgl Library - colors and images
Comparator 排序函数式接口
图扑软件数字孪生海上风电 | 向海图强,奋楫争先
Understanding openstack network
Coinbase将推出首个针对个人投资者的加密衍生产品
[AI player cultivation record] use AI to identify what kind of wealth is next door
10 个 Reduce 常用“奇技淫巧”
Getting started with the go Cobra command line tool
CPU process priority
Quickly understand the commonly used message summarization algorithms, and no longer have to worry about the thorough inquiry of the interviewer
Integrate the authorization interface code of intra city distribution account of multiple express companies nationwide - Express 100
问个sql view的问题
kotlin 异步流
Ask a question about SQL view
Gateway processing flow of zuul source code analysis
I have fundamentally solved the problem of wechat occupying mobile memory