当前位置:网站首页>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
边栏推荐
- One click generation and conversion of markdown directory to word format
- Practical case of SQL optimization: speed up your database
- RichView TRVUnits 图像显示单位
- Redis distributed lock, lock code logic
- Security level
- Introduce reflow & repaint, and how to optimize it?
- How to build a technical team that will bring down the company?
- STL container
- When to catch an exception and when to throw an exception- When to catch the Exception vs When to throw the Exceptions?
- Pytorch register_ Hook (operate on gradient grad)
猜你喜欢

Five ways to query MySQL field comments!

Li Kou Jianzhi offer -- binary tree chapter

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

The application and Optimization Practice of redis in vivo push platform is transferred to the end of metadata by

The MySQL team development specifications used by various factories are too detailed. It is recommended to collect them!
![[download white paper] does your customer relationship management (CRM) really](/img/e3/f130d071afb7309fdbf8a9c65b1d38.jpg)
[download white paper] does your customer relationship management (CRM) really "manage" customers?

Exploration of short text analysis in the field of medical and health (I)

力扣剑指offer——二叉树篇

【附源码】基于知识图谱的智能推荐系统-Sylvie小兔

Can you really learn 3DMAX modeling by self-study?
随机推荐
[技术发展-26]:新型信息与通信网络的数据安全
Stored procedure and stored function in Oracle
CAM Pytorch
Leetcode takes out the least number of magic beans
Pytorch register_ Hook (operate on gradient grad)
runc hang 导致 Kubernetes 节点 NotReady
Timescaledb 2.5.2 release, time series database based on PostgreSQL
Yyds dry inventory swagger positioning problem ⽅ formula
Huawei machine test question: longest continuous subsequence
Win: use PowerShell to check the strength of wireless signal
Talk about the things that must be paid attention to when interviewing programmers
Li Kou Jianzhi offer -- binary tree chapter
[source code attached] Intelligent Recommendation System Based on knowledge map -sylvie rabbit
Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
Which common ports should the server open
Application and Optimization Practice of redis in vivo push platform
RichView TRVUnits 图像显示单位
Restful Fast Request 2022.2.1发布,支持cURL导入
Redis distributed lock, lock code logic
丸子百度小程序详细配置教程,审核通过。