当前位置:网站首页>What is asynchronous operation
What is asynchronous operation
2022-07-26 09:19:00 【Xiaomeng new admission】
1. Background introduction
Synchronization means that only one task can be completed at a time . If there are multiple tasks , You have to line up , The first task is finished , Do the next task , And so on .
Asynchrony means that each task has one or more callback functions (callback), After the end of the previous mission , Not to perform the latter task , Instead, execute the callback function , The latter task is executed before the end of the previous task , So the execution order of the program is inconsistent with the order of the tasks 、 Asynchronous .
2. Knowledge analysis
First js It's a single-threaded language , That is, you can only do one thing at a time . that Js How to implement asynchronous , Isn't asynchrony and single thread self contradictory ? Actually , Single thread and asynchrony really can't be features of a language at the same time .js Chose to become a single threaded language , So it cannot be asynchronous by itself , but js The host environment of ( For example, browser ,Node) It's multi-threaded , The host environment in some way ( Event driven , I'll talk about ) bring js With asynchronous properties .
Browser kernel is multithreaded , They work together under kernel control to keep up with each other , A browser implements at least three resident threads :javascript Engine threads ,UI Rendering thread , Browser event trigger thread .
1.javascript Engine threads are based on event driven single thread execution ,JS The engine is waiting for the task in the task queue , And then deal with it , There is only one browser at any time JS The thread is running JS Program .
2.UI The rendering thread is responsible for rendering the browser interface , When the interface needs to be redrawn (Repaint) Or cause reflux due to an operation (reflow) when , The thread will execute . But we need to pay attention UI Rendering thread and JS Engines are mutually exclusive , When JS Engine execution time UI Thread will be suspended ,UI Updates will be saved in a queue medium to JS Execute immediately when the engine is idle .
3. Event trigger thread , When an event is triggered, the thread will add the event to the end of the queue to be processed , wait for JS Engine handling . These events can come from JavaScript The code block currently executed by the engine is as follows setTimeOut、 Other threads from the browser kernel, such as mouse click 、AJAX Asynchronous request, etc , But because of JS All these events have to be queued JS Engine processing .
3. common problem And Solution
ajax What does the browser do when sending asynchronous requests ?
1.Js Created a ajax request
2. Open another browser ajax Engine threads , perform ajax request
3. After the execution gets the response, put the callback function into the task queue .
4.Js Execute the callback function in the task queue .
What are the common asynchronous callback functions ?
1, Click time ,
2, Timer ,
3,AJAX request .
边栏推荐
- 839. Simulation reactor
- (2006, MySQL server has gone away) problem handling
- Babbitt | metauniverse daily must read: does the future of metauniverse belong to large technology companies or to the decentralized Web3 world
- volatile 靠的是MESI协议解决可见性问题?(上)
- 2022年上海市安全员C证考试试题及模拟考试
- Order based evaluation index (especially for recommendation system and multi label learning)
- ext4文件系统打开了DIR_NLINK特性后,link_count超过65000的后使用link_count=1来表示数量不可知
- Selection and practice of distributed tracking system
- [MySQL] detailed explanation of redo log, undo log and binlog (4)
- 【ARKit、RealityKit】把图片转为3D模型
猜你喜欢

209. Subarray with the smallest length

pycharm 打开多个项目的两种小技巧

Study notes of canal

Zipkin安装和使用

NTT (fast number theory transformation) polynomial inverse 1500 word analysis

(2006, MySQL server has gone away) problem handling

Study notes of dataX

2022 tea artist (intermediate) special operation certificate examination question bank simulated examination platform operation

redis原理和使用-安装和分布式配置

异常处理机制二
随机推荐
JVM command induction
HBuilderX 运行微信开发者工具 “Fail to open IDE“报错解决
(2006,Mysql Server has gone away)问题处理
Laravel框架日志文件存放在哪里?怎么用?
Server memory failure prediction can actually do this!
NTT(快速数论变换)多项式求逆 一千五百字解析
Zipkin安装和使用
756. Serpentine matrix
760. String length
Qtcreator reports an error: you need to set an executable in the custom run configuration
堆外内存的使用
多层嵌套后的 Fragment 懒加载实现
209. Subarray with the smallest length
[leetcode database 1050] actors and directors who have cooperated at least three times (simple question)
省政府召开全省高温天气安全防范工作电视电话会议
Object type collections are de duplicated according to the value of an attribute
语音聊天app源码——钠斯直播系统源码
838. Heap sorting
服务器内存故障预测居然可以这样做!
Original root and NTT 5000 word explanation