当前位置:网站首页>Macro task, micro task and event cycle mechanism
Macro task, micro task and event cycle mechanism
2022-07-25 23:39:00 【An Aquarius procedural ape】
Macro task 、 Micro task and Event cycle mechanism
JavaScript It's single threaded , in other words , At the same time ,JavaScript Only one task can be performed , Other tasks can only wait .
that , Why? JavaScript It's single threaded ?
js It is a script language running in the browser , Because it often involves operation dom, If it's multithreaded , That means , At the same time , Ability to perform multiple tasks .
Just imagine , If a thread modifies dom, Another thread deletes dom, Then the browser doesn't know which operation to perform first .
therefore js The implementation will follow One task, one task .
Why should tasks be divided into synchronous tasks and asynchronous tasks
Just imagine , If js All tasks are synchronized , So when you encounter a timer 、 The type of network delay required to execute the task ?
page May be paralyzed , need It takes a long time to pause and wait for these To execute the completed code
therefore , Asynchronous tasks are introduced .
- Synchronization task : The synchronization task does not need to wait, and the execution result can be seen immediately , such as
console - Asynchronous task : Asynchronous tasks need to wait for a certain time to see the results , such as
setTimeout、Network request
The event loop (Event Loop)
The of event loop is relatively simple , It is one in “JavaScript The engine is waiting for the task ”," Perform tasks " and " Go to sleep and wait for more tasks " An infinite loop of transitions between these States .
The general algorithm of the engine :
- When there are tasks : Start with the first task to enter .
- No other tasks , Sleep until the task appears , Then go to page 1 Step .
Task queue
According to the specification , The event loop is coordinated through the mechanism of task queue . One Event Loop in , There can be one or more task queues (task queue), A task queue is a series of orderly tasks (task) Set ; Each task has a task source (task source), From the same task source task Must be placed in the same task queue , From different sources are added to different queues .setTimeout/Promise etc. API Is the task source .
In the event loop , The key steps for each cycle are as follows :
- In this cycle, select the task that enters the queue first (oldest task), If so, execute ( once )
- Check for presence Micro task (Microtasks), If it exists, it will continue to execute , Until emptied Micro task queue (Microtasks Queue)
- to update render(DOM Rendering )
- The above is a cycle , The main thread repeats the above steps
On the basis of the above cycle, we need to know a few points :
- JS It is divided into synchronous task and asynchronous task
- Synchronization tasks are performed on the main thread , Form an execution stack
- Outside the main thread , The hosting environment manages a task queue , As long as asynchronous tasks have run results , Just put an event in the task queue .
- Once all synchronization tasks in the execution stack are completed ( here JS Engine idle ), The system will read the task queue , Add a runnable asynchronous task to the executable stack , Start execution .
Macro task
(macro)task, It can be understood that the code executed by each stack is a macro task ( It includes getting an event callback from the event queue and putting it into the execution stack to execute ).
| Mission ( Code ) | Macro task | Environmental Science |
|---|---|---|
| script | Macro task | browser |
| event | Macro task | browser |
| Network request (Ajax) | Macro task | browser |
| setTimeout() Timer | Macro task | browser / Node |
| fs.readFile() Read the file | Macro task | Node |
Micro task
Micro task (microtask) Is part of a macro task , Its execution time is after synchronous code execution , Before the next macro task .
Promise.then
Operating mechanism
In the event loop , Each cycle is called tick, every time tick The task processing model of is more complex , But the key steps are as follows :
- Perform a macro task ( Get from the event queue without in the execution stack )
- If micro tasks are encountered during execution , Just add it to the task queue of the micro task
- After the synchronization code in the macro task is executed , Execute all the micro tasks in the current micro task queue immediately ( Execute sequentially )
- The current macro task is finished , Start checking the rendering , then GUI Threads take over rendering
- After rendering ,JS Thread continues to take over , Start the next macro task ( Get... From the event queue )
边栏推荐
猜你喜欢

Node Foundation

POI special effects Market Research

chown: changing ownership of ‘/var/lib/mysql/‘: Operation not permitted

SAP Message No. VG202 IDoc E1EDK18 中付款条款已经转移:检查数据

利用用户脚本优化 Yandere/Konachan 站点浏览体验

【代码案例】博客页面设计(附完整源码)

XxE & XML external entity injection utilization and bypass

Constructors and prototypes

Release of v6.5.1/2/3 series of versions of Xingyun housekeeper: the ability of database OpenAPI continues to be strengthened

LeetCode 0919. 完全二叉树插入器:完全二叉树的数组表示
随机推荐
Discuz atmosphere game style template / imitation lol hero League game DZ game template GBK
2022牛客多校第二场
idea设置get、set模板解决boolean类型字段的命名问题
Serialize operator
Graph traversal DFS, BFS (code explanation)
TS union type
三板斧!助你成为优秀软件工程师
[QNX hypervisor 2.2 user manual]9.6 GDB
2022 Niuke multi School Game 2
TS class
红娘的话
【MUDUO】打包EventLoop和Thread
[QNX Hypervisor 2.2用户手册]9.8 load
[QNX hypervisor 2.2 user manual]9.7 generate
Duplicate numbers in array
PyTorch的数据输入格式要求及转换
WebMvcConfigurationSupport
What is the difference between hot deployment and hot loading?
Ratio of learning_ add,ratio_ subtract,ratio_ multiply,ratio_ Use of divide
[wechat applet] page navigation