当前位置:网站首页>[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
边栏推荐
- L'outil de nettoyage des données flashtext améliore directement l'efficacité de plusieurs dizaines de fois
- 主从复制系统设计
- vulnhub之DC9
- DLA model (classification model + improved segmentation model) + deformable convolution
- Briefly describe the model animation function of unity
- Is it safe for CICC fortune to open an account? I want to open an account to speculate in stocks.
- [fundamentals of image processing] GUI image curve adjustment system based on MATLAB [including Matlab source code 1923]
- 尚硅谷DolphinScheduler视频教程发布
- leetcode:141. Circular linked list [hash table + speed pointer]
- leetcode:6107. 不同骰子序列的数目【dp六个状态 + dfs记忆化】
猜你喜欢

Flashtext, a data cleaning tool, has directly increased the efficiency by dozens of times

Matrix derivation and its chain rule

leetcode:141. 环形链表【哈希表 + 快慢指针】

CVPR 2022 | 美团技术团队精选论文解读

数据治理啥都干

Implementation of collaborative filtering evolution version neuralcf and tensorflow2

Raspberry pie preliminary use

树莓派初步使用

YOLOv6:又快又准的目标检测框架开源啦

数据清洗工具flashtext,效率直接提升了几十倍数
随机推荐
Share three methods of automatic summation in Excel
BS-GX-016基于SSM实现教材管理系统
Is it safe to open an account and buy stocks? Who knows
WordPress collection plug-ins are recommended to be free collection plug-ins
JupyterLab 常用配置
The sharp sword of API management -- eolink
leetcode:141. Circular linked list [hash table + speed pointer]
Briefly describe the model animation function of unity
Is there any risk in opening a new bond registration account? Is it safe?
Unity: 脚本缺失 “The referenced script (Unknown) on this Behaviour is missing!“
【混合编程jni 】第七篇之JNI 的命令行们
FPGA -VGA显示
FPGA -vga display
Introduction to dependency injection in SAP Spartacus
Centos7编译安装Redis
Leetcode (122) - the best time to buy and sell stocks II
Is this a bug? Whether the randomly filled letters can be closed
【图像处理基础】基于matlab GUI图像直方图均衡化系统【含Matlab源码 1924期】
Unity method for setting material and shader
[machine learning] - Introduction to vernacular and explanation of terms