当前位置:网站首页>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
边栏推荐
- PHP Joseph Ring problem
- Use the difference between "Chmod a + X" and "Chmod 755" [closed] - difference between using "Chmod a + X" and "Chmod 755" [closed]
- Limited query of common SQL operations
- Luo Gu Pardon prisoners of war
- Exploration of short text analysis in the field of medical and health (I)
- The most powerful new household god card of Bank of communications. Apply to earn 2100 yuan. Hurry up if you haven't applied!
- Redis' hyperloglog as a powerful tool for active user statistics
- One plus six brushes into Kali nethunter
- [技术发展-26]:新型信息与通信网络的数据安全
- Subject 3 how to turn on the high beam diagram? Is the high beam of section 3 up or down
猜你喜欢

Can you really learn 3DMAX modeling by self-study?

. Net starts again happy 20th birthday

Unpool(nn.MaxUnpool2d)

Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool

Yolov5 model training and detection

Yyds dry inventory swagger positioning problem ⽅ formula

He was laid off.. 39 year old Ali P9, saved 150million

Three properties that a good homomorphic encryption should satisfy
![[OpenGL learning notes 8] texture](/img/77/a4a784a535ea6f4c2382857b266cec.jpg)
[OpenGL learning notes 8] texture

Android advanced interview question record in 2022
随机推荐
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
PHP Joseph Ring problem
Limited query of common SQL operations
Pytorch fine tuning (Fortune): hollowed out design or cheating
Outlook: always prompt for user password
Common bit operation skills of C speech
Action News
Win:将一般用户添加到 Local Admins 组中
Interesting practice of robot programming 16 synchronous positioning and map building (SLAM)
Learn game model 3D characters, come out to find a job?
Grub 2.12 will be released this year to continue to improve boot security
runc hang 导致 Kubernetes 节点 NotReady
A label colorful navigation bar
官宣!第三届云原生编程挑战赛正式启动!
172. Zero after factorial
Advanced learning of MySQL -- Application -- Introduction
Do you know the eight signs of a team becoming agile?
Yyds dry inventory swagger positioning problem ⽅ formula
Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
Win: enable and disable USB drives using group policy