当前位置:网站首页>28. JS implementation mechanism
28. JS implementation mechanism
2022-06-11 20:02:00 【Suyuoa】
JS Allows multiple threads to be created , The concept of synchronization and asynchrony has emerged in multithreading

Let's take a look at the code below

It will print out 1,3, After the 3s Print out after 2
![]()
If we change the timer time to 0 second , Its order is still 1,3,2

![]()
Here the print 1 And 3 It belongs to synchronization task , Timers are asynchronous tasks . All synchronization tasks are placed on the main thread's execution stack , Implementation by item . Asynchronous tasks will be placed in the task queue ( It's also called message queuing ) in
- Asynchronous tasks still have time , Loading images, etc
JS First of all, it will execute Execution stack The synchronization task of , After all synchronization tasks are executed , Start executing asynchronous tasks , Asynchronous tasks are sequentially placed in asynchronous processing processes for parallel processing , When it comes to execution ( For example, it's time , Or click the event to happen ) Then put it into the synchronization task for execution
I now have this code

First JS Will distinguish these four statements , And perform synchronization tasks

- The contents of the asynchronous task queue are allocated by the asynchronous processing process
Now what is displayed on my terminal is 1,4. after JS Processing asynchronous tasks in an asynchronous processing process , After performing the synchronization task, we found that Time is 0 The timer of is triggered preferentially , At this time, the asynchronous processing process will send the callback function to the asynchronous task queue , Then send to the synchronization task queue ( If your two timer events are the same , Then it will be sent to the asynchronous task queue at the same time ( To be exact, the synchronous task queue goes to the asynchronous task queue to find out whether there are any tasks ), The asynchronous task queue will be sent to the synchronous task queue in writing order and then executed )

- At this time, in the synchronization task console.log(1) And console.log(4) It should no longer be in the synchronization task queue , Asynchronous processing of processes console.log(3) Not in an asynchronous process , The tasks in the asynchronous task queue are not transferred to the synchronous task queue console.log(3), I am writing here for the convenience of understanding
So in this step we will print 3, Although printing 3 The writing order is in print 2 After the writing order of
The last thing is to wait console.log(2) Reach time , When the time is reached, it will be put into the synchronization task queue for execution

The execution result of this code is 1,4,3,2
![]()
If it is a click event , After you click it, the callback function will be placed in the asynchronous task queue and then transferred to the synchronous task queue , After being placed in the asynchronous task queue, the asynchronous processing process is not cleared , Because if you click again, it will trigger
Synchronous task queue finding tasks from asynchronous task queue and asynchronous task queue sending tasks to synchronous task queue are infinite cycles , This loop is called an event loop (event loop)
边栏推荐
- Flutter doctor 顯示xcode沒有安裝的解决辦法
- Major work title and requirements of engineering earthquake resistance in autumn 21 [standard answer]
- Yolov3 pytoch code and principle analysis (II): network structure and loss calculation
- Picture bed: picgo+ Tencent cloud +typera
- [advanced MySQL] differences among 10 data types and how to optimize the table structure (3)
- Full list! 2022 Times Asia University Rankings announced!
- Loop filtering to uncover the technical principle behind video thousand fold compression
- Yolov3 pytoch code and principle analysis (I): runthrough code
- moderlarts第一次培训
- Shanghai internal promotion 𞓜 Yuyang teacher's research group of Shanghai Chizhi research institute recruits full-time researchers
猜你喜欢

The tutor transferred me 800 yuan and asked me to simulate a circuit (power supply design)

Yolov3 pytoch code and principle analysis (II): network structure and loss calculation

Linux环境安装mysql数据库详细教程(含卸载和密码重置过程)

Leetcode2027. Minimum number of operations to convert a string (yes, once)

AHB2Standard_ handshake_ Bridge design

STC hardware only automatic download circuit V2

The principle that the picture of QQ Post Bar changes when it is opened

Hanging memory recursive dynamic programming (with example explanation POJ 1163)

POJ 1458 longest common subsequence (dynamic planning exercise)

Apache APISIX 在Amazon Graviton3 上的安装和性能测试
随机推荐
Zzulioj 2903: the power of warriors
2022Redis7.0x版本持久化详细讲解
In 2021, the global barite product revenue was about $571.3 million, and it is expected to reach $710.2 million in 2028
管理者必须知道的三个常识
In 2021, the global revenue of Indian fragrant rice was about $12530 million, and it is expected to reach $21670 million in 2028
WR | 西湖大学鞠峰组微纳塑料污染对人工湿地菌群与脱氮功能的影响
[Err] 1045 - Access denied for user ‘root‘@‘%‘ (using password: YES)
Course design for construction organization, School of distance and continuing education, Dalian University of technology [standard answer]
Leetcode 1992. 找到所有的农场组(可以,一次过)
Questions and requirements of the "high rise building structure" assignment of Dayong in the 21st autumn [standard answer]
Yolov3 pytoch code and principle analysis (II): network structure and loss calculation
iMeta | 南科大夏雨组纳米孔测序揭示微生物可减轻高海拔冻土温室气体排放
巴比特 | 元宇宙每日必读:数字藏品二级市场乱象丛生,00后成新韭菜,监管迫在眉睫?...
Picture bed: picgo+ Tencent cloud +typera
"Video version Mae" of hekaiming team, efficient video pre training! The effect is also very good when mask ratio is up to 90
cocan yocto buildroot
[bug resolution] unpickingerror: a load persistent ID instruction was encoded, but no persistent_ load.
Introduction to jobrunr, a distributed background job scheduler - jaxenter
基于 Vue + Codemirror 实现 SQL 在线编辑器
Volcano engine, Alibaba cloud and Tencent cloud jointly released the 'ultra low delay' live broadcast technical standard