当前位置:网站首页>十八、Kotlin进阶学习:1、挂起函数执行的顺序;2、使用 async 和 await 异步执行挂起函数;3、协程的调度器;4、父子协程;
十八、Kotlin进阶学习:1、挂起函数执行的顺序;2、使用 async 和 await 异步执行挂起函数;3、协程的调度器;4、父子协程;
2022-07-30 05:43:00 【¥伊人独醉】
1、挂起函数执行的顺序;
如果在协程中有多个挂起函数,其执行顺序就是常规代码的顺序
【案例】模拟2个挂起函数


日志:

2、使用 async 和 await 异步执行挂起函数;
根据前面知识知道协程代码在runBlocking中是顺序执行的,使用async可以异步执行,并且与GlobalScope.launch返回job不同,async可以通过await获取返回的值。
并且使用async和await代码可读性更强。
【案例】

日志:

3、协程的调度器;
所谓调度是指让协程在不同的线程上执行。
【案例 】不同的协程在不同的线程上执


日志:

如果不加join(执行顺序不是代码顺序)

4、父子协程;
在协程中启动另一个协程,前者叫父协程,后者叫子协程。当父协程被取消时,所有子协程也会递归地被取消。
例如:第一个请求后,还有若干个请求,但是由于第一个请求失败,之后的请求就没有意义了,所以需要整体取消,使用父子协程解决此类问题。
【案例】

日志:

边栏推荐
- "MySQL Advanced Chapter" four, the storage structure of the index
- 【调优】一个 Spark 任务某天突然变慢怎么解决
- oracle row to column, column to row summary
- torch分布式训练
- Redis 客户端常见异常分析
- Remember a Mailpress plugin RCE vulnerability recurrence
- 【零基础搞定C语言——导航汇总篇】
- nodejs PM2监控及报警邮件发送(二)
- The number of warehouse 】 data quality
- Defense Ideas for a Type of SMS Vulnerability
猜你喜欢

Arrays工具类的使用

sql中 exists的用法

2022CISCNmisc

MySQL - 多表查询与案例详解

C#预定义数据类型简介

Reasons and solutions for Invalid bound statement (not found)
![CTFSHOW command execution [web29-web124] unfinished to be continued](/img/89/786fbe65af4c9f269530bf2d08e1a0.png)
CTFSHOW command execution [web29-web124] unfinished to be continued
![[MATLAB] Image Processing - Recognition of Traffic Signs](/img/45/1a5797a17ebf6db965a64c85e0f037.png)
[MATLAB] Image Processing - Recognition of Traffic Signs

Connect to Mysql in the cloud server Docker detailed graphic and text operations (full)

Online sql editing query tool sql-editor
随机推荐
【OS】操作系统高频面试题英文版(1)
线程的5种状态
C#利用开源NPlot实现K线图(蜡烛图)
MySQL - 函数及约束命令
MySQL storage engine
php-fpm
Monstache执行monstache -f config.toml出错No processor type exists with name [attachment] [type=parse_exc
[Mozhe Academy] Identity Authentication Failure Vulnerability Actual Combat
【Spark】Spark 高频面试题英语版(1)
Flink PostgreSQL CDC configuration and FAQ
Mysql 客户端常见异常分析
Jackson 序列化失败问题-oracle数据返回类型找不到对应的Serializer
C#中对委托的理解和使用
Understand JDBC in one article
Misc-traffic analysis of CTF
TDengine cluster construction
互联网商城盲盒app为何如此火爆
Servlet basic principles and application of common API methods
kali is an essential artifact for information security
Volatility memory forensics - command shows