当前位置:网站首页>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
边栏推荐
- What are the differences between software testers with a monthly salary of 7K and 25K? Leaders look up to you when they master it
- 《上市风云》荐书——唯勇气最可贵
- [Yu Yue education] China Ocean University job search OMG reference
- Network security - firewall
- 微信小程序开发工具 POST net::ERR_PROXY_CONNECTION_FAILED 代理问题
- Anna: Beibei, can you draw?
- Network security - DNS spoofing and phishing websites
- Network security - phishing
- Analysis, use and extension of open source API gateway apisex
- Comment le chef de file gère - t - il l'équipe en cas d'épidémie? Contributions communautaires
猜你喜欢
Solution for processing overtime orders (Overtime unpaid)
机器学习笔记(持续更新中。。。)
PS remove watermark details
How to deal with cache hot key in redis
stm32F407-------DMA
[camera topic] complete analysis of camera dtsi
[shutter] shutter debugging (debugging control related functions | breakpoint management | code operation control)
How do it students find short-term internships? Which is better, short-term internship or long-term internship?
[error record] an error is reported in the fluent interface (no mediaquery widget ancestor found. | scaffold widgets require a mediaquery)
Depth (penetration) selector:: v-deep/deep/ and > > >
随机推荐
stm32F407-------ADC
Rockchip3399 start auto load driver
[fluent] hero animation (hero animation use process | create hero animation core components | create source page | create destination page | page Jump)
[Appendix 6 Application of reflection] Application of reflection: dynamic agent
y54.第三章 Kubernetes从入门到精通 -- ingress(二七)
Network security - man in the middle attack
Learn BeanShell before you dare to say you know JMeter
去除网页滚动条方法以及内外边距
[camera topic] complete analysis of camera dtsi
Internal connection query and external connection
MySQL learning 03
Bottleneck period must see: how can testers who have worked for 3-5 years avoid detours and break through smoothly
LabVIEW安装第三方VISA软件后NI VISA失效
Network security - vulnerabilities and Trojans
《上市风云》荐书——唯勇气最可贵
单词单词单词
Introduce in detail how to communicate with Huawei cloud IOT through mqtt protocol
His experience in choosing a startup company or a big Internet company may give you some inspiration
PS remove watermark details
Network security - scanning and password explosion 2