async与launch一样都是开启一个协程,但是async会返回一个Deferred对象,该Deferred也是一个job

async函数类似于 launch函数.它启动了一个单独的协程,这是一个轻量级的线程并与其它所有的协程一起并发的工作.不同之处在于 launch 返回一个 Job 并且不附带任何结果值,而 async 返回一个 Deferred —— 一个轻量级的非阻塞 future,这代表了一个将会在稍后提供结果的 promise.你可以使用 .await() 在一个延期的值上得到它的最终结果, 但是 Deferred 也是一个 Job

看一下async的使用:

GlobalScope.launch {
var deffer1 = async(Dispatchers.IO) {
async1()
}.await()
var deffer2 = async(Dispatchers.IO) {
async2()
}.await()
val result = deffer1 + deffer2
println("the sum is: $result")
} private suspend fun async1(): Int {
delay(1500)
println("async1")
return 1
}
private suspend fun async2(): Int {
delay(300)
println("async2")
return 2
}

最终输出:

I/System.out: async1
I/System.out: async2
I/System.out: the sum is: 3

async1先执行,说明其是顺序进行的,因为await本身就是一个挂起行数,这时候它的用法与withContext没有什么差别。

再看一个例子:

GlobalScope.launch {
var deffer1 = async(Dispatchers.IO) {
async1()
}
var deffer2 = async(Dispatchers.IO) {
async2()
}
Log.d("GlobalScope","before sum")
var r1 = deffer1.await()
var r2 = deffer2.await()
val result = r1 + r2
Log.d("GlobalScope","the sum is -> $result")
} private suspend fun async1(): Int {
delay(1500)
Log.d("GlobalScope","async1")
return 1
}
private suspend fun async2(): Int {
delay(300)
Log.d("GlobalScope","async2")
return 2
}

打印log:

D/GlobalScope: before sum
D/GlobalScope: async2
D/GlobalScope: async1
D/GlobalScope: the sum is -> 3

可以看到async2先执行,因为挂起函数在后面,await是挂起函数。

简化写法:

GlobalScope.launch {
var deffer1 = async(Dispatchers.IO) {
async1()
}
var deffer2 = async(Dispatchers.IO) {
async2()
}
Log.d("GlobalScope","before sum")
val result = deffer1.await() + deffer2.await()
Log.d("GlobalScope","the sum is -> $result")
}

打印log:

D/GlobalScope: before sum
D/GlobalScope: async2
D/GlobalScope: async1
D/GlobalScope: the sum is -> 3

协程的async使用的更多相关文章

  1. Python PEP 492 中文翻译——协程与async/await语法

    原文标题:PEP 0492 -- Coroutines with async and await syntax 原文链接:https://www.python.org/dev/peps/pep-049 ...

  2. Python的异步编程[0] -> 协程[0] -> 协程和 async / await

    协程 / Coroutine 目录 生产者消费者模型 从生成器到异步协程– async/await 协程是在一个线程执行过程中可以在一个子程序的预定或者随机位置中断,然后转而执行别的子程序,在适当的时 ...

  3. 协程 和 async await

    协程, 是 为了 避免 闭包传递变量 的 性能损耗 而产生  . 如果不是 为了 避免 闭包传递变量 的 性能损耗 ,    线程池 和 Task 已经够了,  不需要 再设计 出 协程 来  . 闭 ...

  4. 通过async实现协程的延迟执行及结果获取

    在上一次https://www.cnblogs.com/webor2006/p/12022065.html对于协程的async和wait进行了初步的学习,其可以加速执行的性能,其实对于async它是提 ...

  5. 深入理解协程(三):async/await实现异步协程

    原创不易,转载请联系作者 深入理解协程分为三部分进行讲解: 协程的引入 yield from实现异步协程 async/await实现异步协程 本篇为深入理解协程系列文章的最后一篇. 从本篇你将了解到: ...

  6. async 异步协程进阶

    协程通过 async/await 语法进行声明,是编写异步应用的推荐方式 例如新定义一个协程(coroutine object): async def foo(): return 42 首先先来介绍下 ...

  7. Python3的原生协程(Async/Await)和Tornado异步非阻塞

    原文转载自「刘悦的技术博客」https://v3u.cn/a_id_113 我们知道在程序在执行 IO 密集型任务的时候,程序会因为等待 IO 而阻塞,而协程作为一种用户态的轻量级线程,可以帮我们解决 ...

  8. [转载]Python 3.5 协程究竟是个啥

    http://blog.rainy.im/2016/03/10/how-the-heck-does-async-await-work-in-python-3-5/ [译] Python 3.5 协程究 ...

  9. [译] Python 3.5 协程究竟是个啥

    转自:http://blog.rainy.im/2016/03/10/how-the-heck-does-async-await-work-in-python-3-5/ [译] Python 3.5 ...

  10. Python 原生协程------asyncio

    协程 在python3.5以前,写成的实现都是通过生成器的yield from原理实现的, 这样实现的缺点是代码看起来会很乱,于是3.5版本之后python实现了原生的协程,并且引入了async和aw ...

随机推荐

  1. node+express+ multer 实现文件上传入门

    文件上传 文件上传需要借助一个中间件 multer 因此我们需要安装 cnpm install multer --save 前端界面 在express创建的项目下的 public/upload目录下创 ...

  2. ant-design-vue 表单验证详解

    表单验证详解 <template> <!-- 第一个坑 :model="formState.youForm" 一定要写成这样 不要写成:model="f ...

  3. Vue基础系列文章05----babel安装与使用

    1.安装babel-node包,运行: 1) npm install --save-dev @babel/core @babel/cli @babel/preset-env @babel/node 2 ...

  4. Protobuf示例:Golang and Python

    之前的文章中已经展示过如何在C++中使用protobuf,本文将简单示范protobuf在Golang和Python中的使用. Talk is cheap. Show you my code. 首先是 ...

  5. 【1】Anaconda安装超简洁教程,配置环境、创建虚拟环境、添加镜像源

    相关文章: [1]Anaconda安装超简洁教程,瞬间学会! [2]Anaconda下:ipython文件的打开方式,Jupyter Notebook中运行.py文件,快速打开ipython文件的方法 ...

  6. 13.4 DirectX内部劫持绘制

    相对于外部绘图技术的不稳定性,内部绘制则显得更加流程与稳定,在Dx9环境中,函数EndScene是在绘制3D场景后,用于完成将最终的图像渲染到屏幕的一系列操作的函数.它会将缓冲区中的图像清空,设置视口 ...

  7. 给Java小白,整理一套能上手的简单编程算法题!!!

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 数学离程序员有多近? ifelse也好.for循环也罢,代码可以说就是对数学逻辑的具 ...

  8. 图解Java原理

    1:强制类型转换数据溢出 2:顺序结构的流程图 3:单if语句的流程图 4:标准if-else语句的流程图 5:扩展if-else语句的流程图 6:for循环流程图

  9. Ubuntu22.04 & Win11 双系统hibernate热切换实现

    Ubuntu22.04 & Win11 双系统hibernate热切换实现 目录 Ubuntu22.04 & Win11 双系统hibernate热切换实现 修改交换分区或交换文件 修 ...

  10. idea 集成接口测试插件

    idea api集成接口测试 日常逼逼叨 相信很多后端开发接口的小伙伴们在开发完成后也会进行简单的测试,可能会用到apifox,postman之类的测试工具,但是up近期发现了一个比较好用的idea插 ...