当前位置:网站首页>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
边栏推荐
- RichView TRVStyle MainRVStyle
- Binary tree traversal - middle order traversal (golang)
- [download white paper] does your customer relationship management (CRM) really "manage" customers?
- MATLB | multi micro grid and distributed energy trading
- Chinese natural language processing, medical, legal and other public data sets, sorting and sharing
- Asynchronous and promise
- Pytorch register_ Hook (operate on gradient grad)
- The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
- Which common ports should the server open
- Common bit operation skills of C speech
猜你喜欢
Visual studio 2019 set transparent background (fool teaching)
Official announcement! The third cloud native programming challenge is officially launched!
[Digital IC hand tearing code] Verilog edge detection circuit (rising edge, falling edge, double edge) | topic | principle | design | simulation
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Learn game model 3D characters, come out to find a job?
One plus six brushes into Kali nethunter
Traditional chips and AI chips
Yyds dry inventory swagger positioning problem ⽅ formula
"2022" is a must know web security interview question for job hopping
JVM - when multiple threads initialize the same class, only one thread is allowed to initialize
随机推荐
使用druid連接MySQL數據庫報類型錯誤
Huawei machine test question: longest continuous subsequence
Win:使用 PowerShell 检查无线信号的强弱
PowerShell:在代理服务器后面使用 PowerShell
Start the remedial work. Print the contents of the array using the pointer
Yolov5 model training and detection
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
WCF: expose unset read-only DataMember property- WCF: Exposing readonly DataMember properties without set?
Grpc message sending of vertx
Action News
February database ranking: how long can Oracle remain the first?
JVM - when multiple threads initialize the same class, only one thread is allowed to initialize
220213c language learning diary
Some query constructors in laravel (2)
RichView TRVStyle MainRVStyle
Application and Optimization Practice of redis in vivo push platform
如何搭建一支搞垮公司的技術團隊?
Unified blog writing environment
A label making navigation bar
A label colorful navigation bar