当前位置:网站首页>【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 官网
边栏推荐
- [mixed programming JNI] Part 9: JNI summary
- 中金证券经理给的开户二维码办理股票开户安全吗?我想开个户
- [fundamentals of image processing] GUI image curve adjustment system based on MATLAB [including Matlab source code 1923]
- leetcode:141. Circular linked list [hash table + speed pointer]
- 【混合编程jni 】第十一篇之JNA详情
- Yolov6: the fast and accurate target detection framework is open source
- 【混合编程jni 】第七篇之JNI 的命令行们
- leetcode:1567. 乘积为正数的最长子数组长度【dp[i]表示以i结尾的最大长度】
- AI智能抠图工具--头发丝都可见
- Unity布料系統_Cloth組件(包含動態調用相關)
猜你喜欢
leetcode:6107. 不同骰子序列的数目【dp六个状态 + dfs记忆化】
[mathematical modeling] spanning tree based on Matlab GUI random nodes [including Matlab source code 1919]
[fundamentals of image processing] GUI image histogram equalization system based on MATLAB [including Matlab source code 1924]
VB. Net class library (Advanced - 2 overload)
YOLOv6:又快又准的目標檢測框架開源啦
YOLOv6:又快又准的目标检测框架开源啦
VB. Net class library (advanced version - 1)
MATLAB与Mysql数据库连接并数据交换(基于ODBC)
Leetcode (452) - detonate the balloon with the minimum number of arrows
经典Wide & Deep模型介绍及tensorflow 2代码实现
随机推荐
Pass note 【 dynamic planning 】
Flower shop window layout [dynamic planning]
在哪家券商公司开户最方便最安全可靠
DLA model (classification model + improved segmentation model) + deformable convolution
How to write test cases and a brief introduction to go unit test tool testify
Unity method for setting material and shader
[mixed programming JNI] Part 9: JNI summary
尚硅谷DolphinScheduler视频教程发布
不花一分钱做个在线的gif合成服务
Do an online GIF synthesis service at no cost
简析攻防演练中蓝队的自查内容
What are the test case design methods?
Leetcode (452) - detonate the balloon with the minimum number of arrows
leetcode:1567. Length of the longest subarray whose product is a positive number [dp[i] indicates the maximum length ending with I]
[cloud native topic -51]:kubesphere cloud Governance - operation - step by step deployment of microservice based business applications - database middleware redis microservice deployment process
Introduction to operator
Usage of MGrid in numpy
The network connection is disconnected. Please refresh and try again
Centos7 compiling and installing redis
Different subsequence problems I