当前位置:网站首页>Kotlin - 协程 Coroutine
Kotlin - 协程 Coroutine
2022-07-05 02:17:00 【懒到死的程序员】
概念
- 协程:本质就是线程框架。一套基于 Thread(线程) 封装的 工具API(就像 Java 的 Executor 、Android 的 AsyncTask Handler 以及 RxJava)。
- 挂起函数:
- 挂起的本质:切线程执行完后会自动切回来继续。
- 挂起函数挂起的是当前在执行的协程,然后执行自己的协程。此时被挂起的协程和执行它的线程会分离,线程继续去执行其他任务或无事可干(就是它原本的样子,回收或再利用),当挂起函数执行完会切回来继续执行之前被挂起的协程。这些协程执行在各自指定的线程中(创建时通过Dispatchers调度而不是Thread,因为会切回来而不是单纯指定线程),它们被写在同一代码块里有直观的先后逻辑顺序。
- 由于消除了回调嵌套可以自定义挂起函数:
- 什么时候定义:需要做耗时操作的时候(I/O、计算、等待)才会挂起当前的协程。
- suspend 关键字:用来限制该函数只能在协程里调用或者在其他挂起函数里调用,因为“挂起->执行完->切回去”只有在协程中使用才能实现。真正挂起操作靠的是最终调用的那个协程自带的挂起函数。也有提醒“这是一个耗时操作,是挂起函数要在协程中使用”的意思。
- 非阻塞式:本质就是不卡线程。对比其他基于 Java 的多线程解决方案,协程的优点是借助 Kotlin 语言的优势,让我们方便在同一代码块(作用域)里做线程切换(看起来同步的方式写出异步代码),即原先串行写的代码现在并行来写(同一代码块里的代码是在同一线程执行(单线程是阻塞式的做耗时操作就会卡线程,切线程就不会卡之前的线程),Java切线程会回调里嵌套回调关系复杂,而协程通过挂起函数切换线程执行完会返回来继续,同代码块里上下行连续写,逻辑直观并消除模板代码),消除多线程异步之间协作任务的操作难度。
协程作用域 CoroutineScope
协程上下文 CoroutineContext
协程创建 CoroutineBuilder
挂起函数 Suspend Function
ViewModelKTX
LifeCycleKTX
边栏推荐
- 85.4% mIOU! NVIDIA: using multi-scale attention for semantic segmentation, the code is open source!
- Uniapp navigateto jump failure
- Abacus mental arithmetic test
- February database ranking: how long can Oracle remain the first?
- Pytorch common code snippet collection
- Runc hang causes the kubernetes node notready
- Subject 3 how to turn on the high beam diagram? Is the high beam of section 3 up or down
- Talk about the things that must be paid attention to when interviewing programmers
- 172. Zero after factorial
- Visual explanation of Newton iteration method
猜你喜欢
Visual studio 2019 set transparent background (fool teaching)
PowerShell: use PowerShell behind the proxy server
Three properties that a good homomorphic encryption should satisfy
Tucson will lose more than $400million in the next year
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Mysql database | build master-slave instances of mysql-8.0 or above based on docker
One plus six brushes into Kali nethunter
Stored procedure and stored function in Oracle
Application and Optimization Practice of redis in vivo push platform
Naacl 2021 | contrastive learning sweeping text clustering task
随机推荐
MATLB|多微电网及分布式能源交易
Uniapp navigateto jump failure
Yyds dry inventory jetpack hit dependency injection framework Getting Started Guide
Introduce reflow & repaint, and how to optimize it?
Yolov5 model training and detection
力扣剑指offer——二叉树篇
"C zero foundation introduction hundred knowledge and hundred cases" (72) multi wave entrustment -- Mom shouted for dinner
Win:使用 PowerShell 检查无线信号的强弱
RichView TRVStyle MainRVStyle
Bert fine tuning skills experiment
Summary and practice of knowledge map construction technology
Application and Optimization Practice of redis in vivo push platform
Learn tla+ (XII) -- functions through examples
Outlook:总是提示输入用户密码
Incremental backup? db full
Three properties that a good homomorphic encryption should satisfy
Official announcement! The third cloud native programming challenge is officially launched!
He was laid off.. 39 year old Ali P9, saved 150million
Chinese natural language processing, medical, legal and other public data sets, sorting and sharing
Visual studio 2019 set transparent background (fool teaching)