当前位置:网站首页>Kotlin - suspend function
Kotlin - suspend function
2022-07-23 13:47:00 【A lazy programmer】
The function of the suspend function
Cut threads to do other things , Return with the result after completion , Continue to work from the previous progress .
Suspend the recovery process
① Suspended function suspends its coroutine ( Directly wrap the coroutine of the hanging function , There are nested processes ), Therefore, the suspended function can only be called in the coroutine or other suspended functions .
- At this time, the suspended collaboration : The code will not continue to execute ( That is, those written under the suspend function , If the code above the hang function is not executed , The suspended function specifies that it will be executed in the same thread after the suspended function is executed , Specifies that concurrent execution continues on other threads ).
- At this time, the thread of the suspended collaboration : Instead of blocking, continue to perform other tasks or have nothing to do ( It was originally scheduled by the system , Recycle or reuse ).
② If the collaboration is suspended, a Continuation( Similar to the progress archive point in the game ), Then the suspend function executes its own coroutine in the thread it specifies ( Task code ).
③ After the execution of the suspend function , adopt Continuation.resume() Pass your own result and return it to the collaboration it belongs to , Continue from the previous progress .
Non-blocking type
The essence is not to jam threads ( Single threads are blocking , The following code will not be executed until the current task is completed , If you switch to another thread, the thread before the card will not be processed ). Compare other based on Java Multithreading solutions for , The advantage of synergetic process is to rely on Kotlin Language advantages and suspend recovery function , Let's make it convenient to use the same code block ( Scope ) In, the task code is written in logical order, but it is executed in different threads ( Write asynchronous code in a way that looks synchronous ), That is, the code originally written in serial is now written in parallel (Java Thread cutting will cause complex callback relationships to be nested in callbacks , Each coroutine is executed in its own designated thread ( Created by Dispatchers Dispatch , instead of Thread Because it is not simply specifying threads ), The elimination callback looks like synchronous execution in up-down order , Logical intuition and elimination of template code ), Eliminate the operation difficulty of multi-threaded asynchronous collaborative tasks ( Others call it without considering which thread the task is executed on ).
Custom suspend function
Due to the elimination of callback nesting, you can customize the hang function :
- When to define : When time-consuming operations are needed (I/O、 Calculation 、 wait for ) Will suspend the current collaboration .
- What has been solved : The creator informed that this was a time-consuming operation , It also specifies that the process is executed in the background thread , Thread safety of the caller is guaranteed .
- suspend keyword : It is used to restrict the function to be called only in the coroutine or in other suspended functions , because “ Hang up -> After execution -> Cut back ” It can only be achieved by using it in the collaboration process . The real hang operation depends on the hang function of the coroutine finally called . There are also reminders “ It's a time-consuming operation , Is the hang function to be used in the coroutine ” It means .
Suspend function Suspend Function
All suspended functions in the official framework can be cancelled .
| suspend | Suspend current process , And save its state ( Variable ). There can only be coroutines or other pending function calls . |
| resume | Return to the last pending collaboration , And resume execution from the previous state . |
delay() | It will delay the execution time of the collaboration , The current thread will not be blocked , Continue to execute the cooperation process after completion . |
| withContext | |
| suspendCoroutine() |
suspendCoroutine()
suspend fun main() {
println("AAA")
suspendCoroutine<Unit> { continuation ->
continuation.resume(Unit)
}
println("BBB")
}边栏推荐
- 4D antenna array layout design
- Light chain dissection / tree chain dissection
- In depth interpretation of EVM's ecological Empire
- 基于BIM+3DGIS的智慧城市基础设施管理
- 接口测试-简单的接口自动化测试Demo
- General contents of radar introduction column
- China leads the United States in another emerging technology field and stands at the commanding height of scientific and technological innovation
- Smart canteen data analysis system
- Interviewer: have you learned about the underlying implementation of reentrantlock? tell us your opinion
- 【深入浅出玩转FPGA学习10------简单的Testbench设计】
猜你喜欢

反常积分的审敛

【深入浅出玩转FPGA学习10------简单的Testbench设计】

In depth interpretation of EVM's ecological Empire

了解canvas

docker mysql

Point target simulation of SAR imaging (II) -- matlab simulation

freemarker

第二章关系数据库课后习题

Method of entering mathematical formula into mark down document

Ti single chip millimeter wave radar code walk (XXV) -- angular dimension (3D) processing flow
随机推荐
数据库系统原理与应用教程(047)—— MySQL 查询(九):连接查询
Chapter II relational database after class exercises
php连接sql server
Unity制作简单拦截近防炮——如何预测打击目标
数据库系统原理与应用教程(042)—— MySQL 查询(四):使用通配符构造查询条件
微信小程序--动态设置导航栏颜色
LeetCode_52_N皇后Ⅱ
Point target simulation of SAR imaging (I) -- mathematical model
QNX modify system time
【MUDUO】EPOLLPOLLER事件分发器
MySQL面试题
C#做一个简单浏览器
Introduction to radar part vii 4 SAR system design
Point target simulation of SAR imaging (III) -- Analysis of simulation results
ROS2自学笔记:Gazebo物理仿真平台
PHP获取当前时间戳三位毫秒 - 毫秒时间戳
prometheus+node-exporter+grafana 监控服务器系统资源
Running matlab program on GPU
[Part 2] full analysis of oak-d+turnlebot3 robot project
【cocos creator】spine动画,监听播放结束