当前位置:网站首页>Kotlin - coroutine
Kotlin - coroutine
2022-07-05 02:18:00 【A lazy programmer】
Concept
- coroutines : The essence is the thread framework . A set of based on Thread( Threads ) Packaged Tools API( It's like Java Of Executor 、Android Of AsyncTask Handler as well as RxJava).
- Suspend function :
- The nature of suspension : After the thread is executed, it will automatically cut back to continue .
- The suspended function suspends the currently executing coroutine , Then execute your own coordination process . At this point, the suspended coroutine and the thread executing it will be separated , The thread continues to perform other tasks or has nothing to do ( It's the way it was , Recycle or reuse ), When the suspended function finishes executing, it will cut back to continue executing the suspended coroutine . These coroutines are executed in their respective designated threads ( Created by Dispatchers Schedule instead of Thread, Because it will cut back rather than simply specify the thread ), They are written in the same code block with intuitive logical order .
- Due to the elimination of callback nesting, you can customize the hang function :
- When to define : When time-consuming operations are needed (I/O、 Calculation 、 wait for ) Will suspend the current collaboration .
- suspend keyword : It is used to restrict the function to be called only in the coroutine or in other suspended functions , because “ Hang up -> After execution -> Cut back ” It can only be achieved by using it in the collaboration process . The real hang operation depends on the hang function of the coroutine finally called . There are also reminders “ It's a time-consuming operation , Is the hang function to be used in the coroutine ” It means .
- Non-blocking type : The essence is not to jam threads . Compare with others based on Java Multithreading solutions for , The advantage of synergetic process is to rely on Kotlin The advantages of language , Let's make it convenient to use the same code block ( Scope ) Thread switching in ( It seems that asynchronous code is written synchronously ), That is, the code originally written in serial is now written in parallel ( The code in the same code block is executed in the same thread ( A single thread is a blocking operation, and it will jam the thread if it does time-consuming operations , Cutting threads will not block the threads before ),Java Thread cutting will cause complex callback relationships to be nested in callbacks , The coroutine switches the thread by suspending the function, and will return after execution to continue , Write up and down in the same code block , Logical intuition and elimination of template code ), Eliminate the operation difficulty of multi-threaded asynchronous collaborative tasks .
Process scope CoroutineScope
Collaboration context CoroutineContext
The process creates CoroutineBuilder
Suspend function Suspend Function
ViewModelKTX
LifeCycleKTX
边栏推荐
- [download white paper] does your customer relationship management (CRM) really "manage" customers?
- R语言用logistic逻辑回归和AFRIMA、ARIMA时间序列模型预测世界人口
- Win:使用组策略启用和禁用 USB 驱动器
- Huawei machine test question: longest continuous subsequence
- One plus six brushes into Kali nethunter
- Prometheus monitors the correct posture of redis cluster
- Win: enable and disable USB drives using group policy
- Learn tla+ (XII) -- functions through examples
- Which common ports should the server open
- [机缘参悟-38]:鬼谷子-第五飞箝篇 - 警示之一:有一种杀称为“捧杀”
猜你喜欢
MATLB | multi micro grid and distributed energy trading
力扣剑指offer——二叉树篇
Go RPC call
Practical case of SQL optimization: speed up your database
[source code attached] Intelligent Recommendation System Based on knowledge map -sylvie rabbit
"2022" is a must know web security interview question for job hopping
Introduce reflow & repaint, and how to optimize it?
Three properties that a good homomorphic encryption should satisfy
丸子百度小程序详细配置教程,审核通过。
"C zero foundation introduction hundred knowledge and hundred cases" (72) multi wave entrustment -- Mom shouted for dinner
随机推荐
Exploration of short text analysis in the field of medical and health (II)
Last week's hot review (2.7-2.13)
Talk about the things that must be paid attention to when interviewing programmers
Can you really learn 3DMAX modeling by self-study?
STL container
STM32 series - serial port UART software pin internal pull-up or external resistance pull-up - cause problem search
ICSI 311 Parser
Interpretation of mask RCNN paper
Introduce reflow & repaint, and how to optimize it?
PowerShell:在代理服务器后面使用 PowerShell
如何搭建一支搞垮公司的技术团队?
Android advanced interview question record in 2022
Educational Codeforces Round 122 (Rated for Div. 2) ABC
Yyds dry inventory jetpack hit dependency injection framework Getting Started Guide
Visual explanation of Newton iteration method
Runc hang causes the kubernetes node notready
Using druid to connect to MySQL database reports the wrong type
Win:使用组策略启用和禁用 USB 驱动器
【附源码】基于知识图谱的智能推荐系统-Sylvie小兔
R语言用logistic逻辑回归和AFRIMA、ARIMA时间序列模型预测世界人口