当前位置:网站首页>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
边栏推荐
- Last week's hot review (2.7-2.13)
- [source code attached] Intelligent Recommendation System Based on knowledge map -sylvie rabbit
- MATLB | multi micro grid and distributed energy trading
- Yyds dry inventory swagger positioning problem ⽅ formula
- Valentine's Day flirting with girls to force a small way, one can learn
- What is the length of SHA512 hash string- What is the length of a hashed string with SHA512?
- Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery
- How to make a cool ink screen electronic clock?
- 187. Repeated DNA sequence - with unordered_ Map basic content
- Pytorch common code snippet collection
猜你喜欢
Win:使用 PowerShell 检查无线信号的强弱
Exploration of short text analysis in the field of medical and health (II)
CAM Pytorch
Unpool(nn.MaxUnpool2d)
MATLB | multi micro grid and distributed energy trading
Practice of tdengine in TCL air conditioning energy management platform
The steering wheel can be turned for one and a half turns. Is there any difference between it and two turns
Open source SPL optimized report application coping endlessly
One plus six brushes into Kali nethunter
Security level
随机推荐
Valentine's Day flirting with girls to force a small way, one can learn
【附源码】基于知识图谱的智能推荐系统-Sylvie小兔
Win:使用组策略启用和禁用 USB 驱动器
MySQL regexp: Regular Expression Query
Go RPC call
Yolov5 model training and detection
PowerShell: use PowerShell behind the proxy server
Do you know the eight signs of a team becoming agile?
Win:使用 PowerShell 检查无线信号的强弱
A label making navigation bar
Bert fine tuning skills experiment
Incremental backup? db full
85.4% mIOU! NVIDIA: using multi-scale attention for semantic segmentation, the code is open source!
Subject 3 how to turn on the high beam diagram? Is the high beam of section 3 up or down
Learn tla+ (XII) -- functions through examples
A label colorful navigation bar
Luo Gu Pardon prisoners of war
[技术发展-26]:新型信息与通信网络的数据安全
February database ranking: how long can Oracle remain the first?
The most powerful new household god card of Bank of communications. Apply to earn 2100 yuan. Hurry up if you haven't applied!