当前位置:网站首页>[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 :
suspendHow 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
边栏推荐
- How to enable Hana cloud service on SAP BTP platform
- CVPR 2022 - Interpretation of selected papers of meituan technical team
- Parsing complex JSON in fluent
- Vulnhub's DC8
- 【题解】剑指 Offer 15. 二进制中1的个数(C语言)
- [fundamentals of image processing] GUI image histogram equalization system based on MATLAB [including Matlab source code 1924]
- Design of master-slave replication system
- 360手机助手首家接入APP签名服务系统 助力隐私安全分发
- leetcode - 买卖股票的最佳时机
- AI intelligent matting tool - hair can be seen
猜你喜欢

leetcode:6103. 从树中删除边的最小分数【dfs + 联通分量 + 子图的值记录】

树莓派初步使用

简述unity的模型动画功能

Unity: 脚本缺失 “The referenced script (Unknown) on this Behaviour is missing!“

网络爬虫2:抓取网易云音乐评论用户ID及主页地址

【图像处理基础】基于matlab GUI图像曲线调整系统【含Matlab源码 1923期】

YOLOv6:又快又准的目標檢測框架開源啦

Leetcode (763) -- dividing letter ranges

论文解读(LG2AR)《Learning Graph Augmentations to Learn Graph Representations》

VB. Net class library to obtain the color under the mouse in the screen (Advanced - 3)
随机推荐
Final part of web crawler: send directional messages to 100000 Netease cloud users
C语言:简单计算器多次使用代码实现
Is there any risk for flush to register and open an account? Is it safe?
[cloud native topic -51]:kubesphere cloud Governance - operation - step by step deployment of microservice based business applications - database middleware redis microservice deployment process
leetcode:152. 乘积最大子数组【考虑两个维度的dp】
同花顺注册开户有没有什么风险?安全吗?
Design of master-slave replication system
CVPR 2022 - Interpretation of selected papers of meituan technical team
Data governance does everything
美术向的Unity动画知识
【混合编程jni 】第七篇之JNI 的命令行们
中金财富开户安全吗?我想开户炒股。
WP collection plug-in tutorial no thanks for WordPress collection of rules
random_normal_initializer 使用
Introduction to dependency injection in SAP Spartacus
Which securities company is the most convenient, safe and reliable for opening an account
Implementation of collaborative filtering evolution version neuralcf and tensorflow2
大龄程序员的一些出路
在线协作文档综合评测 :Notion、FlowUs、Wolai、飞书、语雀、微软 Office、谷歌文档、金山文档、腾讯文档、石墨文档、Dropbox Paper、坚果云文档、百度网盘在线文档
Unity3d plug-in anyportrait 2D bone animation