当前位置:网站首页>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()
}边栏推荐
- Integrated API interface code of domestic express companies for intra city distribution and ordering - Express 100
- Why did the audio and video based cloud conference usher in a big explosion of development?
- How long will it take to open a mobile account? Is online account opening safe?
- Vipshop's "special sale" business is no longer easy to do?
- I have fundamentally solved the problem of wechat occupying mobile memory
- Golden age ticket: Web3.0 Security Manual
- Geological disaster early warning monitoring RTU
- AGCO AI frontier promotion (6.24)
- How can junior middle school developers effectively reduce their own workload?
- Why does the kubernetes environment require that bridge NF call iptables be enabled?
猜你喜欢

CVPR 2022 | interprétation de certains documents de l'équipe technique de meituan

硬件开发笔记(六): 硬件开发基本流程,制作一个USB转RS232的模块(五):创建USB封装库并关联原理图元器件

使用 Abp.Zero 搭建第三方登录模块(一):原理篇

面试官:MySQL 数据库查询慢,除了索引问题还可能是什么原因?

Huawei PC grows against the trend, and product power determines everything

Golden age ticket: Web3.0 Security Manual

工业物联网(IIoT)的八个主要趋势

每日一题day8-515. 在每个树行中找最大值

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

1. Snake game design
随机推荐
Why did the audio and video based cloud conference usher in a big explosion of development?
ERR AUTH&lt; password&gt; called without anypassword configured for the default user. Ar
华为AppLinking中统一链接的创建和使用
Kotlin interface generic covariant inversion
源碼解析 Handler 面試寶典
Getting started with the go Cobra command line tool
AGCO AI frontier promotion (6.24)
Implement Domain Driven Design - use ABP framework - update operational entities
CVPR 2022 | 美团技术团队精选论文解读
10 reduce common "tricks"
Cmput 379 explanation
我从根上解决了微信占用手机内存问题
快速了解常用的消息摘要算法,再也不用担心面试官的刨根问底
Manuel d'entrevue du gestionnaire de l'analyse des sources
系统测试主要步骤
kotlin 初始化块
Creation and use of unified links in Huawei applinking
Beauty of script │ VBS introduction interactive practice
《中国数据库安全能力市场洞察,2022》报告研究正式启动
Can inspection results be entered after the completion of inspection lot UD with long-term inspection characteristics in SAP QM?