当前位置:网站首页>【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 behavior is missing“
- Centos7 compiling and installing redis
- 【混合编程jni 】第九篇之Jni总结
- 中金证券经理的开户链接开户买股票安全吗?有谁知道啊
- CVPR 2022 - Interpretation of selected papers of meituan technical team
- Unity3d plug-in anyportrait 2D bone animation
- Module externe unity3d anyportrait 2D Skeleton Animation
- [mixed programming JNI] Part 7: JNI command lines
- Flutter 中 ValueNotifier<List<T>> 监听问题解决
- [fundamentals of image processing] GUI image curve adjustment system based on MATLAB [including Matlab source code 1923]
猜你喜欢

Yolov6: the fast and accurate target detection framework is open source

数据清洗工具flashtext,效率直接提升了几十倍数

VB. Net class library to obtain the color under the mouse in the screen (Advanced - 3)
![Flower shop window layout [dynamic planning]](/img/d9/6b8f9cd0f74e70b313d2571c2ded30.png)
Flower shop window layout [dynamic planning]

The sharp sword of API management -- eolink

Matrix derivation and its chain rule

Product design in the extreme Internet Era
![[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)

數據清洗工具flashtext,效率直接提昇了幾十倍數

What are the test case design methods?
随机推荐
不花一分钱做个在线的gif合成服务
Flashtext, a data cleaning tool, has directly increased the efficiency by dozens of times
Restfultoolkitx of idea utility plug-in -- restful interface debugging
Unity3D插件 AnyPortrait 2D骨骼动画制作
在哪家券商公司开户最方便最安全可靠
nmap参数详解
FPGA -VGA显示
[mathematical modeling] spanning tree based on Matlab GUI random nodes [including Matlab source code 1919]
What is the “ How to remove a custom form list?
Pass note 【 dynamic planning 】
Is there any risk in opening a securities registration account? Is it safe?
在哪个平台买股票开户最安全?求分享
Common configuration of jupyterlab
leetcode:141. Circular linked list [hash table + speed pointer]
MATLAB and MySQL database connection and data exchange (based on ODBC)
Different subsequence problems I
Which securities company is the most convenient, safe and reliable for opening an account
尚硅谷DolphinScheduler视频教程发布
【BUG反馈】WebIM在线聊天系统发消息时间问题
协同过滤进化版本NeuralCF及tensorflow2实现