当前位置:网站首页>[kotlin] keyword suspend learning of thread operation and async understanding
[kotlin] keyword suspend learning of thread operation and async understanding
2022-06-26 22:33:00 【ChrisEighteen18】
background
stay kotlin To learn some of the methods used and practical applications :
suspend
How to use it in the end ?
package im.zego.takeaguess.logic.sdk.interfaces
import android.app.Application
import im.zego.zim.entity.ZIMMessage
import kotlinx.coroutines.flow.FlowCollector
interface IZIMManager {
fun init(application: Application)
fun unInit()
/** * Sign in * * @param userId * @param userName * @param token */
suspend fun login(userId: String, userName: String, token: String): Result<Unit>
/** * Log out * */
fun logout()
/** * Create and join a room * * @param roomId * @param roomName */
suspend fun createRoom(roomId: String, roomName: String): Result<Unit>
/** * Join a room * * @param roomId */
suspend fun joinRoom(roomId: String): Result<Unit>
/** * Leave the room * * @param roomId */
suspend fun logoutRoom(roomId: String): Result<Unit>
/** * Observe room message changes * * @param collector */
suspend fun observeRoomMessage(collector: FlowCollector<List<ZIMMessage>>)
}
Answer questions and solve doubts
suspend Use of functions
The main thing is to learn a async Deep understanding of asynchronous processing , The Chinese meaning of this keyword is : Hang . This makes me think directly about the thread hang , Release resources when you need them . Actually in kotlin China is much the same , However, the method used is quite special ; and java It's different .
After realizing the above interface Interface It can be used as shown in the following code ;
The basic logic is to use keywords async
Asynchronous processing . This saves resources and time .
val loginZimRoomDef = async(Dispatchers.IO) {
zimManager.createRoom(
roomId = joinRoomResp.roomInfo.roomId,
roomName = joinRoomResp.roomInfo.name
)
} // establish ZIM room
val loginExpressRoomDef = async(Dispatchers.IO) {
expressManager.loginRoom(
roomId = joinRoomResp.roomInfo.roomId,
userId = AccountStore.userId,
userName = AccountStore.userName,
token = token
)
} // Get into Express room
val initCopyrightedMusicDef = async(Dispatchers.IO) {
copyrightMusicManager.init(
userId = AccountStore.userId, userName = AccountStore.userName
)
The above code can implement multiple initialization methods at the same time .
Please refer to for more usage Composing suspending functions -kotlin Official website
边栏推荐
- CVPR 2022 | 美团技术团队精选论文解读
- curl: (35) LibreSSL SSL_ connect: SSL_ ERROR_ SYSCALL in connection
- Detailed explanation of nmap parameters
- Leetcode (763) -- dividing letter ranges
- [fundamentals of image processing] GUI image curve adjustment system based on MATLAB [including Matlab source code 1923]
- [mathematical modeling] spanning tree based on Matlab GUI random nodes [including Matlab source code 1919]
- Yolov6: the fast and accurate target detection framework is open source
- JupyterLab 常用配置
- Which securities company is the most convenient, safe and reliable for opening an account
- 同花顺注册开户有没有什么风险?安全吗?
猜你喜欢
L'outil de nettoyage des données flashtext améliore directement l'efficacité de plusieurs dizaines de fois
leetcode:6103. 从树中删除边的最小分数【dfs + 联通分量 + 子图的值记录】
leetcode:152. Product maximum subarray [consider DP of two dimensions]
【图像处理基础】基于matlab GUI图像曲线调整系统【含Matlab源码 1923期】
【LeetCode】1984. Minimum difference between highest and lowest of K scores
网络爬虫2:抓取网易云音乐评论用户ID及主页地址
Data governance does everything
leetcode:710. 黑名单中的随机数【映射思维】
在Flutter中解析复杂的JSON
FPGA -VGA显示
随机推荐
What are the test case design methods?
Introduction of classic wide & deep model and implementation of tensorflow 2 code
亿级月活全民K歌Feed业务在腾讯云MongoDB中的应用及优化实践
Yolov6: un cadre de détection de cibles rapide et précis est Open Source
【混合编程jni 】第七篇之JNI 的命令行们
Test comparison of linear model LN, single neural network SNN, deep neural network DNN and CNN
Data governance does everything
VB. Net class library (advanced version - 1)
Which securities company is the most convenient, safe and reliable for opening an account
如何写好测试用例以及go单元测试工具testify简单介绍
數據清洗工具flashtext,效率直接提昇了幾十倍數
random_normal_initializer 使用
Some ways out for older programmers
分享三种在Excel表格中自动求和的方法
Word chess based on heuristic search
curl: (35) LibreSSL SSL_ connect: SSL_ ERROR_ SYSCALL in connection
在哪家券商公司开户最方便最安全可靠
买股票通过中金证券经理的开户二维码开户资金是否安全?想开户炒股
这个算BUG吗?乱填的字母是否可以关闭
Partage de trois méthodes de sommation automatique dans un tableau Excel