当前位置:网站首页>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 .
边栏推荐
猜你喜欢

redis原理和使用-基本特性

OFDM 十六讲- OFDM

Ext4 file system opens dir_ After nlink feature, link_ Use link after count exceeds 65000_ Count=1 means the quantity is unknown

Vertical search

【Mysql】一条SQL语句是怎么执行的(二)

CF1481C Fence Painting

原根与NTT 五千字详解

The Child and Binary Tree-多项式开根求逆

服务器内存故障预测居然可以这样做!

JVM command induction
随机推荐
语音聊天app源码——钠斯直播系统源码
Sending and receiving of C serialport
Hbuilderx runs the wechat developer tool "fail to open ide" to solve the error
2B和2C
Two tips for pycharm to open multiple projects
Redis principle and usage - installation and distributed configuration
Object 的Wait Notify NotifyAll 源码解析
力扣——二叉树剪枝
redis原理和使用-安装和分布式配置
力扣题DFS
Elastic APM安装和使用
Flask project learning (I) -- sayhello
pycharm 打开多个项目的两种小技巧
Thread Join 和Object wait 的区别
论文笔记: 知识图谱 KGAT (未完暂存)
[MySQL] detailed explanation of MySQL lock (III)
Simple message mechanism of unity
Li Mu D2L (V) -- multilayer perceptron
[arkit, realitykit] turn pictures into 3D models
[leetcode database 1050] actors and directors who have cooperated at least three times (simple question)