当前位置:网站首页>Kotlin middle process understanding and Practice (I)
Kotlin middle process understanding and Practice (I)
2022-07-03 02:07:00 【yu-Knight】
Kotlin Understanding and practice of Zhongxie Cheng ( One )
What is a journey
Coroutines are thread based , It is a lightweight thread
stay Android What kind of problem is the middle process used to solve
1. Processing time-consuming operations , Such tasks often block the main thread .
2. Keep the main thread safe , That is to ensure that any... Is called safely from the main thread suspend function .
Coroutines is :
Coroutines synchronize asynchronous logic , Put an end to hell .
The core of the collaborative process is , Functions or a program can be suspended , Restore in the suspended location later .
# Suspension and recovery of collaboration
Basic operations of general functions include :invoke( or call) and return, The program added suspend and resume:
suspend - Also known as suspend or pause , Used to pause the execution of the current program , And save all local variables ;
resume - Used to allow a suspended process to resume execution from where it was suspended
Suspend function
1. Use suspend Keyword decorated functions are called suspended functions .
2. Suspended functions can only be called within a coroutine body or other suspended functions
Scheduler
All collaborations must run in the scheduler , Even if they run on the main thread
-Dispatchers.Main
Android Main thread on , Used for processing UI Interaction and some lightweight tasks ,
call suspend function
call UI function
to update LiveData
-Dispatchers.IO
Non main thread , Designed for disk and network IO To optimize
call suspend function
call UI function
to update LiveData
-Dispatchers.Default
Non main thread , Specially designed for CPU Intensive tasks are optimized
Array sorting
JSON Data analysis
Deal with differences and judge
Two parts of the process
Kotlind There are two levels to realize the co process
1. Infrastructure layer , Standard library collaboration API, It mainly provides the most basic conceptual and semantic support for collaborative process
2. Business framework layer , The upper framework of the collaborative process supports
Mission leaks
1. When a collaborative task is lost , Unable to track , Will cause memory 、CPU、 Disk and other resources are wasted , Even sending a useless network request , This situation is called task leakage .
2. In order to avoid the coprocess leak ,Kotlin Structured concurrency mechanism is introduced .
Structured concurrency
Using structured concurrency can :
1. Cancel the task , Cancel a task when it is no longer needed .
2. Tracking mission , When the task is in progress , Track it .
3. Send out the wrong signal , When the coroutine fails , An error signal indicates that an error has occurred .
CoroutinScope
One . Defining a coroutine must specify its CoroutineScope. It tracks all collaborations , It can also cancel all the coroutines it started .
Two . Common related API Yes :
1.GlobalScope, The life cycle is process Grade , Even if Activity perhaps Fragment Has been destroyed , The collaboration process is still being implemented .
2.MainScope, stay Activity Use in , Can be in onDestroy() Cancel the cooperation process .
3.viewModelScope, Only in ViewModel Use in , binding ViewModel Life cycle of .
4.lifecycleScope, Only in Activity、Fragment Use in , Will bind Activity and Fragment Life cycle of .
Start and cancel the process
1. Start the coroutines : Start the builder , Boot mode , Scope builder ,Job Life cycle of
2. Cancel the program : Cancellation of the agreement ,CPU Intensive tasks are cancelled , Side effects of synergetic cancellation , Overtime task
边栏推荐
- File class (check)
- CFdiv2-Fixed Point Guessing-(区间答案二分)
- LabVIEW安装第三方VISA软件后NI VISA失效
- Network security - talking about security threats
- Machine learning notes (constantly updating...)
- 苏世民:25条工作和生活原则
- Introduce in detail how to communicate with Huawei cloud IOT through mqtt protocol
- Summary of ES6 filter() array filtering methods
- udp接收队列以及多次初始化的测试
- Wechat applet development tool post net:: err_ PROXY_ CONNECTION_ Failed agent problem
猜你喜欢
Ni visa fails after LabVIEW installs the third-party visa software
Button button adaptive size of wechat applet
Analysis, use and extension of open source API gateway apisex
MySQL学习03
【Camera专题】OTP数据如何保存在自定义节点中
Everything file search tool
Query product cases - page rendering data
[camera topic] how to save OTP data in user-defined nodes
In the face of difficult SQL requirements, HQL is not afraid
Asian Games countdown! AI target detection helps host the Asian Games!
随机推荐
Redis:Redis的简单使用
Y54. Chapter III kubernetes from introduction to mastery -- ingress (27)
技术大佬准备就绪,话题C位由你决定
MySQL学习03
elastic stack
单词单词单词
"Jetpack - livedata parsing"
In 2022, 95% of the three most common misunderstandings in software testing were recruited. Are you that 5%?
2022 financial product revenue ranking
Query product cases - page rendering data
Introduce in detail how to communicate with Huawei cloud IOT through mqtt protocol
Explore the conversion between PX pixels and Pt pounds, mm and MM
Network security - vulnerabilities and Trojans
Network security - scan
File class (add / delete)
Network security - dynamic routing protocol rip
[fluent] hero animation (hero animation use process | create hero animation core components | create source page | create destination page | page Jump)
DQL basic operation
LabVIEW安装第三方VISA软件后NI VISA失效
Summary of ES6 filter() array filtering methods