当前位置:网站首页>【Kotlin】关键词suspend 线程操作的学习和async理解
【Kotlin】关键词suspend 线程操作的学习和async理解
2022-06-26 22:22:00 【ChrisEighteen18】
背景
在kotlin 的项目中学习一些使用的方法和实际的应用:
suspend到底怎么使用呢?
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()
/** * 登录 * * @param userId * @param userName * @param token */
suspend fun login(userId: String, userName: String, token: String): Result<Unit>
/** * 退出登录 * */
fun logout()
/** * 创建并加入一个房间 * * @param roomId * @param roomName */
suspend fun createRoom(roomId: String, roomName: String): Result<Unit>
/** * 加入一个房间 * * @param roomId */
suspend fun joinRoom(roomId: String): Result<Unit>
/** * 离开房间 * * @param roomId */
suspend fun logoutRoom(roomId: String): Result<Unit>
/** * 观测房间消息变化 * * @param collector */
suspend fun observeRoomMessage(collector: FlowCollector<List<ZIMMessage>>)
}
答疑解惑
suspend函数的使用
主要是学习到一个async异步处理的深刻理解,这个关键词的中文意思是:悬挂。这让我直接就想到线程的挂起,要用的时候再释放资源。其实在kotlin中也是大同小异,不过使用的方法就比较特殊;和java就不太一样。

在实现了上述的接口Interface可以如下代码所示进行使用;
基本的逻辑就是一定要使用到关键词async进行异步处理。这可以节省资源和时间。
val loginZimRoomDef = async(Dispatchers.IO) {
zimManager.createRoom(
roomId = joinRoomResp.roomInfo.roomId,
roomName = joinRoomResp.roomInfo.name
)
} // 创建ZIM房间
val loginExpressRoomDef = async(Dispatchers.IO) {
expressManager.loginRoom(
roomId = joinRoomResp.roomInfo.roomId,
userId = AccountStore.userId,
userName = AccountStore.userName,
token = token
)
} // 进入Express房间
val initCopyrightedMusicDef = async(Dispatchers.IO) {
copyrightMusicManager.init(
userId = AccountStore.userId, userName = AccountStore.userName
)
上述代码可以同时实现多个初始化的方法。
更多的用法请参考Composing suspending functions -kotlin 官网
边栏推荐
- Unity: 脚本缺失 “The referenced script (Unknown) on this Behaviour is missing!“
- 协同过滤进化版本NeuralCF及tensorflow2实现
- Installation avec homebrew dans un environnement Mac OS [email protected]
- [mixed programming JNI] Part 7: JNI command lines
- 在哪家券商公司开户最方便最安全可靠
- 在Flutter中解析复杂的JSON
- [mixed programming JNI] Part 12 jnaerator
- Microservices, an important part of cloud native architecture
- 中金证券经理给的开户二维码办理股票开户安全吗?我想开个户
- Flashtext, a data cleaning tool, has directly increased the efficiency by dozens of times
猜你喜欢

FPGA -VGA显示
![[mathematical modeling] spanning tree based on Matlab GUI random nodes [including Matlab source code 1919]](/img/0c/17efaaa2488451b6dd15d9db33eba7.jpg)
[mathematical modeling] spanning tree based on Matlab GUI random nodes [including Matlab source code 1919]

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

Raspberry pie preliminary use
![[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)

Introduction of classic wide & deep model and implementation of tensorflow 2 code

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

Comprehensive evaluation of online collaboration documents: note, flowus, WOLAI, Feishu, YuQue, Microsoft office, Google Docs, Jinshan docs, Tencent docs, graphite docs, Dropbox paper, nutcloud docs,

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

Some ways out for older programmers
随机推荐
leetcode:6103. 从树中删除边的最小分数【dfs + 联通分量 + 子图的值记录】
Briefly describe the model animation function of unity
经典Wide & Deep模型介绍及tensorflow 2代码实现
L'outil de nettoyage des données flashtext améliore directement l'efficacité de plusieurs dizaines de fois
leetcode - 买卖股票的最佳时机
简述unity的模型动画功能
360 mobile assistant is the first to access the app signature service system to help distribute privacy and security
Test comparison of linear model LN, single neural network SNN, deep neural network DNN and CNN
[mixed programming JNI] Part 6: operation of strings and arrays in native
不花一分钱做个在线的gif合成服务
Introduction of classic wide & deep model and implementation of tensorflow 2 code
Web crawler 2: crawl the user ID and home page address of Netease cloud music reviews
numpy中mgrid的用法
Unity3D插件 AnyPortrait 2D骨骼動畫制作
网络爬虫2:抓取网易云音乐评论用户ID及主页地址
【数学建模】基于matlab GUI随机节点的生成树【含Matlab源码 1919期】
线性模型LN、单神经网络SNN、深度神经网络DNN与CNN测试对比
同花顺注册开户有没有什么风险?安全吗?
Introduction to dependency injection in SAP Spartacus
Système de distribution Unity Composants en tissu (y compris les dépendances d'appel dynamique)