当前位置:网站首页>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)
边栏推荐
- Implementation of SQL online editor based on Vue + codemirror
- 09-MySQL锁
- unity里面使用opencv,qt建立plugin(一)
- Off line operation of situation and policy (version) of Dayong in autumn 21 [standard answer]
- C#深拷贝
- AHB_ Bus_ Matrix_ 3x3 design
- 接口隔离原则
- A brief talk on shutter button
- In 2021, the global revenue of Indian fragrant rice was about $12530 million, and it is expected to reach $21670 million in 2028
- Detailed explanation on persistence of 2022redis7.0x
猜你喜欢

Shanghai internal promotion 𞓜 Yuyang teacher's research group of Shanghai Chizhi research institute recruits full-time researchers

28. JS执行机制

Detailed explanation on persistence of 2022redis7.0x

556. next larger element iii- (31. next permutation) - two iterations

A Mechanics-Informed Artificial Neural Network Approach in Data-Driven Constitutive Modeling 学习

Richter's substitution principle

巴比特 | 元宇宙每日必读:数字藏品二级市场乱象丛生,00后成新韭菜,监管迫在眉睫?...

Pyqt5 tips - button vertical display method, QT designer sets button vertical display.

In 2021, the global adult diaper revenue was about $11560million, which is expected to reach $15440million in 2028. From 2022 to 2028, the CAGR was 4.2%

AHB2Standard_ handshake_ Bridge design
随机推荐
谷歌提出超强预训练模型CoCa,在ImageNet上微调Top-1准确率达91%!在多个下游任务上SOTA!...
Off line operation of situation and policy (version) of Dayong in autumn 21 [standard answer]
使用flask框架写挡板
Golang学习笔记—基础篇
全名单!2022泰晤士亚洲大学排名公布!
Flutter doctor 顯示xcode沒有安裝的解决辦法
POJ 1458 longest common subsequence (dynamic planning exercise)
AHB2APB_ Bridge design
Course design for construction organization, School of distance and continuing education, Dalian University of technology [standard answer]
27. this指向问题
ICML 2022 | 基于结构化数据的异常检测再思考:我们究竟需要怎样的图神经网络?...
Pyqt5 tips - button vertical display method, QT designer sets button vertical display.
模块八作业
基于 Vue + Codemirror 实现 SQL 在线编辑器
秀创意,赢显卡!MMPose姿态估计创意大赛震撼来袭
管理者必须知道的三个常识
[Err] 1045 - Access denied for user ‘root‘@‘%‘ (using password: YES)
接口隔离原则
Unsupervised image classification code analysis notes of scan:learning to classify images without (1): simclr
In 2021, the global cement and aggregate revenue will be about USD 204320 million, and it is expected to reach USD 242670 million in 2028