当前位置:网站首页>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
边栏推荐
- PHP Joseph Ring problem
- Video display and hiding of imitation tudou.com
- A tab Sina navigation bar
- RichView TRVStyle MainRVStyle
- "C zero foundation introduction hundred knowledge and hundred cases" (72) multi wave entrustment -- Mom shouted for dinner
- Outlook: always prompt for user password
- Redis' hyperloglog as a powerful tool for active user statistics
- 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
- Naacl 2021 | contrastive learning sweeping text clustering task
猜你喜欢
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Exploration and practice of integration of streaming and wholesale in jd.com
Win: use PowerShell to check the strength of wireless signal
"2022" is a must know web security interview question for job hopping
Subject 3 how to turn on the high beam diagram? Is the high beam of section 3 up or down
Li Kou Jianzhi offer -- binary tree chapter
Mysql database | build master-slave instances of mysql-8.0 or above based on docker
Three properties that a good homomorphic encryption should satisfy
Exploration of short text analysis in the field of medical and health (I)
Interesting practice of robot programming 14 robot 3D simulation (gazebo+turtlebot3)
随机推荐
Pgadmin 4 V6.5 release, PostgreSQL open source graphical management tool
Interpretation of mask RCNN paper
[illumination du destin - 38]: Ghost Valley - chapitre 5 Flying clamp - one of the Warnings: There is a kind of killing called "hold Kill"
JVM - when multiple threads initialize the same class, only one thread is allowed to initialize
[understanding of opportunity -38]: Guiguzi - Chapter 5 flying clamp - warning one: there is a kind of killing called "killing"
CAM Pytorch
[技术发展-26]:新型信息与通信网络的数据安全
Subject 3 how to turn on the high beam diagram? Is the high beam of section 3 up or down
Stored procedure and stored function in Oracle
One plus six brushes into Kali nethunter
Restful Fast Request 2022.2.1发布,支持cURL导入
Es uses collapsebuilder to de duplicate and return only a certain field
Visual explanation of Newton iteration method
Advanced conditional statements of common SQL operations
"2022" is a must know web security interview question for job hopping
. Net starts again happy 20th birthday
Mysql database | build master-slave instances of mysql-8.0 or above based on docker
STM32 series - serial port UART software pin internal pull-up or external resistance pull-up - cause problem search
Missile interception -- UPC winter vacation training match
Valentine's Day flirting with girls to force a small way, one can learn