当前位置:网站首页>[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
边栏推荐
- [hybrid programming JNI] details of JNA in Chapter 11
- Matrix derivation and its chain rule
- 尚硅谷DolphinScheduler视频教程发布
- WordPress collection plug-ins are recommended to be free collection plug-ins
- 树莓派初步使用
- leetcode:1567. 乘积为正数的最长子数组长度【dp[i]表示以i结尾的最大长度】
- Centos7编译安装Redis
- leetcode - 买卖股票的最佳时机
- Common configuration of jupyterlab
- 这个算BUG吗?乱填的字母是否可以关闭
猜你喜欢
![[solution] sword finger offer 15 Number of 1 in binary (C language)](/img/ab/149775ae8ed94464efdf6921c1022a.png)
[solution] sword finger offer 15 Number of 1 in binary (C language)
![[fundamentals of image processing] GUI image curve adjustment system based on MATLAB [including Matlab source code 1923]](/img/e8/6342f2dc6e7f06a847852ce4b40719.jpg)
[fundamentals of image processing] GUI image curve adjustment system based on MATLAB [including Matlab source code 1923]

L'outil de nettoyage des données flashtext améliore directement l'efficacité de plusieurs dizaines de fois

CVPR 2022 - Interpretation of selected papers of meituan technical team

【LeetCode】1984. Minimum difference between highest and lowest of K scores

【数学建模】基于matlab GUI随机节点的生成树【含Matlab源码 1919期】

360手机助手首家接入APP签名服务系统 助力隐私安全分发

Leetcode (763) -- dividing letter ranges

亿级月活全民K歌Feed业务在腾讯云MongoDB中的应用及优化实践

Restfultoolkitx of idea utility plug-in -- restful interface debugging
随机推荐
WordPress collection plug-ins are recommended to be free collection plug-ins
LabVIEW Arduino tcp/ip remote smart home system (project part-5)
Leetcode - the best time to buy or sell stocks
尚硅谷DolphinScheduler视频教程发布
Share three methods of automatic summation in Excel
主从复制系统设计
Solution of valuenotifier < list < t > > monitoring problem in fluent
L'outil de nettoyage des données flashtext améliore directement l'efficacité de plusieurs dizaines de fois
Is there any risk in opening a new bond registration account? Is it safe?
[mixed programming JNI] Part 7: JNI command lines
数据清洗工具flashtext,效率直接提升了几十倍数
Parsing complex JSON in fluent
树莓派初步使用
AI intelligent matting tool - hair can be seen
leetcode:6103. 从树中删除边的最小分数【dfs + 联通分量 + 子图的值记录】
【BUG反馈】WebIM在线聊天系统发消息时间问题
YOLOv6:又快又准的目標檢測框架開源啦
MATLAB and MySQL database connection and data exchange (based on ODBC)
【混合编程jni 】第九篇之Jni总结
买股票通过中金证券经理的开户二维码开户资金是否安全?想开户炒股