当前位置:网站首页>Kotlin coroutine suspend function suspend keyword
Kotlin coroutine suspend function suspend keyword
2022-06-13 00:46:00 【Ango cannot move】
suspend The pending process
What is the schedule
Namely launch{
}
or
async{
}
Things in the code block
Used suspend after A coroutine will be switched out to perform the operation . Not in the current thread
suspend It's a temporary cut . Cut back after the execution .
launch(Dispatchers.Main) {
// Switch to the sub thread for operation
val repoResponse: Response<FeedBackLists>
withContext(Dispatchers.IO) {
repoResponse = RetrofitClient
.instance
.getApi()
//suspend The suspended method must be called in Ctrip
.getFeedBack(1, 1)
}
// Or in the main thread
binding.tvText.text = "repoResponse :${repoResponse.data!!.data[0].content}"
}Hanging can be understood as opening a thread . Or cut a thread , It's just that after the function is suspended , The coroutine will automatically switch back to the original thread .
So the so-called hang . Thread switching . This cut back operation
It is called in the thread resume
however suspend No thread switching .
First, customize a suspend function

Then the main thread calls

You will find that it is still running on the main thread .

withContext You can switch to a child thread
So suspend Switching threads does not happen when the external pending function is called . It is withContext When called
withContext There is no direct thread switching . Still in there
In grammar suspend What's the use of keywords ?
remind , The creator of the function =》 User's reminder
Remind that this is a time-consuming function that is automatically placed in the background .
This reminder can make the main thread not stuck
When to use suspend keyword
Time consuming operations such as io、 File read and write , Network interaction , Blur or beautify the picture .
Waiting situation Such as deley(10000) The long-running
边栏推荐
- 三角波与三角波卷积
- Easyexcel read excel simple demo
- Win10 home vs pro vs enterprise vs enterprise LTSC
- 今日睡眠质量记录74分
- 市值破万亿,连续三个月销量破10万,比亚迪会成为最强国产品牌?
- ROS2之OpenCV人脸识别foxy~galactic~humble
- Packaging and uplink of btcd transaction process (III)
- After so long use, CSDN has finally opened a blog
- Kotlin 协程的作用域构建器 coroutineScope与runBlocking 与supervisorScope,协程同步运行,协程挂掉的时候其他协程如何不被挂掉。
- RCC clock configuration of stm32f401
猜你喜欢

Druid reports an error connection holder is null

What is pytorch? Explain the basic concepts of pytorch

深度学习每周期的步数多少合适?

MAYA建模六

How to solve the duplication problem when MySQL inserts data in batches?

Transaction creation of btcd transaction process (I)

深度学习训练多少轮?迭代多少次?

MySQL lpad() and rpad() concatenate string functions with specified length

ROS2之OpenCV人脸识别foxy~galactic~humble

kotlin 协程withContext切换线程
随机推荐
Cve-2021-24078 vulnerability analysis
Andersen Global通过在芬兰和丹麦的合作协议拓展北欧地区业务版图
通过抓包下载钉钉直播回放
Map from getting started to performance optimization
[sca-cnn interpretation] spatial and channel wise attention
How many steps are appropriate for each cycle of deep learning?
阿姨学代码续集:能力吊打大批程序员
Three column simple Typecho theme lanstar/ Blue Star Typecho theme
Arduino controls tb6600 driver +42 stepper motor
@Disallowcurrentexecution prevents quartz scheduled tasks from executing in parallel
【北亚服务器数据恢复】虚拟机文件丢失导致Hyper-V服务瘫痪的数据恢复案例
Set sail
[gxyctf2019] no dolls -- detailed explanation
The origin of MySQL in bedtime stories
Transaction verification of btcd transaction process (2)
Arduino control tm1637 common positive four digit nixie tube
[ciscn2019 North China Day2 web1]hack world --buuctf
What is pytorch? Explain the basic concepts of pytorch
Summary of openstack installation problems
MCU serial port interrupt and message receiving and sending processing -- judge and control the received information