当前位置:网站首页>JS single thread understanding notes - Original
JS single thread understanding notes - Original
2022-07-27 12:44:00 【Hua Weiyun】
One 、 what JavaScript A single thread ?
JavaScript One of the characteristics of language is single thread , in other words , Only one thing can be done at a time . that , Why? JavaScript There can't be more than one thread ? This can improve efficiency .
JavaScript The single thread , Related to its use . As a browser scripting language ,JavaScript Its main purpose is to interact with users , And operation DOM. This determines that it can only be a single thread , Otherwise, it will bring complicated synchronization problems . such as , Assume JavaScript There are two threads at the same time , A thread in a DOM Add content on node , Another thread deleted this node , Which thread should the browser follow ?
therefore , To avoid complexity , From the birth ,JavaScript It's a single thread , This has become the core feature of the language , It won't change in the future .
Two 、JavaScript A single thread , How to execute asynchronous code ?
Single thread means , All tasks are queued , The previous task is over , Will perform the latter task . If the previous task took a long time , The latter task had to wait .
Message queue : Message queuing is a first in first out queue , It contains all kinds of messages .
The event loop : Event loop means that the main thread repeatedly fetches messages from the message queue 、 The process of execution .
actually , The main thread does only one thing , It is to get messages from the message queue 、 Execution message , Message again 、 Re execution . When the message queue is empty , Will wait until the message queue becomes non empty . And the main thread can only execute the current message after , To get a message . This mechanism is called the event loop mechanism , The process of taking a message and executing it is called a loop .
3、 ... and 、 Event cycle mechanism
The above figure roughly describes :
- When the main thread runs, it will produce an execution stack , The code in the stack calls some api when , They add various events to the event queue ( When the trigger conditions are met , Such as setTimeout completion of enforcement )
- And the code in the stack is executed , It will read the events in the event queue , To execute those callbacks
- So circular
Be careful , Always wait for the code in the stack to execute before reading the events in the event queue
边栏推荐
- POJ1548 Robots【二分图最小路径覆盖】
- How to use the server to build our blog
- Necessary foundation: Signature Verification
- An overview of kernel compilation system
- 20210512 recursive formula
- Isolation level
- Xposed+FDex2 app脱壳 (黑猫投诉app脱壳)
- The strongest distributed locking tool: redisson
- Detail throw and throws
- Cvpr22 | graph neural architecture search of relational consciousness
猜你喜欢

Interviewer: how to deal with the data loss of redis master-slave cluster switching?

HDU1698_Just a Hook

堆

SparkSubmit.main()方法提交外部参数,远程提交standalone集群任务

multi-table query

Openpyxl drawing area map

Basic architecture of data Lake

Necessary foundation: Signature Verification

Map interface

The song of the virtual idol was originally generated in this way!
随机推荐
String to realize fuzzy query
Getting started for beginners: build your own blog with WordPress
Xianghe meat cake in memory
Top 10 international NFT exchanges
Detail the construction methods, attributes and common methods in reflection
QT | qcheckbox of control
4. Analysis of the execution process of make modules for general purposes
视频游戏沉迷行为研究综述
An overview of kernel compilation system
Complete data summary of lapsus$apt organization that stole Microsoft's source code in March 2022
12 pictures, take you to thoroughly understand ZGC garbage collector!
Pyside6/pyqt development experience summary (2) - set shortcut keys
flinksql从Oracle同步数据到Doris,一共50几个字段,Oracle表中3000多万条
SparkSubmit.main()方法提交外部参数,远程提交standalone集群任务
[Nuxt 3] (十二) 项目目录结构 2
Interviewer: how can you close an order without using a scheduled task?
Application parameters of Southern biotech HRP labeled avidin avidin avidin
Insert sort summary
Gartner 权威预测未来4年网络安全的8大发展趋势
sql 语句问题, 求计算相差10分钟以内的数据作为同一批次数据显示