当前位置:网站首页>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
边栏推荐
- Use the difference between "Chmod a + X" and "Chmod 755" [closed] - difference between using "Chmod a + X" and "Chmod 755" [closed]
- The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
- Phpstrom setting function annotation description
- Rabbit MQ message sending of vertx
- Huawei machine test question: longest continuous subsequence
- Codeforces Round #770 (Div. 2) ABC
- Introduce reflow & repaint, and how to optimize it?
- 187. Repeated DNA sequence - with unordered_ Map basic content
- One click generation and conversion of markdown directory to word format
- "2022" is a must know web security interview question for job hopping
猜你喜欢
Phpstrom setting function annotation description
Go RPC call
Visual explanation of Newton iteration method
Tucson will lose more than $400million in the next year
Introduce reflow & repaint, and how to optimize it?
Stored procedure and stored function in Oracle
JVM's responsibility - load and run bytecode
runc hang 导致 Kubernetes 节点 NotReady
[source code attached] Intelligent Recommendation System Based on knowledge map -sylvie rabbit
Do you know the eight signs of a team becoming agile?
随机推荐
Outlook: always prompt for user password
Pytorch register_ Hook (operate on gradient grad)
Summary and practice of knowledge map construction technology
How to build a technical team that will bring down the company?
Use the difference between "Chmod a + X" and "Chmod 755" [closed] - difference between using "Chmod a + X" and "Chmod 755" [closed]
PHP Basics - detailed explanation of DES encryption and decryption in PHP
RichView TRVStyle MainRVStyle
Yolov5 model training and detection
I use these six code comparison tools
Naacl 2021 | contrastive learning sweeping text clustering task
What is the length of SHA512 hash string- What is the length of a hashed string with SHA512?
如何搭建一支搞垮公司的技术团队?
JVM - when multiple threads initialize the same class, only one thread is allowed to initialize
February database ranking: how long can Oracle remain the first?
Erreur de type de datagramme MySQL en utilisant Druid
如何搭建一支搞垮公司的技術團隊?
Interesting practice of robot programming 16 synchronous positioning and map building (SLAM)
Common bit operation skills of C speech
Restful Fast Request 2022.2.1发布,支持cURL导入
Subject 3 how to turn on the high beam diagram? Is the high beam of section 3 up or down