In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
How does javascript achieve asynchronism? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
We know that javascript is single-threaded, but why are so many event handling asynchronous? The single thread of javascript means that there is only one main thread to interpret and execute the javascript code. So how does javascript achieve asynchronism?
Asynchronous process
First, the main thread initiates an asynchronous request, and the corresponding worker thread receives the request and processes it. During that time, the main thread sends the request and then goes to do something else. When the processing of the worker thread has a result, a thread is assigned within the browser to notify the main thread that the asynchronous request that has just been initiated has a result (this notification process actually pushes the callback function into the message queue, also known as the event queue, also known as the task queue). When the main thread finishes processing the task in the current call stack, it will read the message from the message queue, that is, the callback will be called. This completes a cycle of reading messages.
The mechanism by which javascript executes code is the process of constantly reading message callbacks from message queues and executing them. Because each message (or every callback) is triggered by a corresponding event (such as mouse clicks, ajax, timer events, etc.), this process is called an event loop.
Asynchronous task
In the process of pushing messages to the event queue above, if the worker thread processes both macro and micro tasks, the macro task is given priority, and then the message of the corresponding macro task is pushed to the macro task message queue; then the worker thread processes the micro task, and then pushes the message of the corresponding micro task to the message queue of the micro task.
Note that the macro task message queue and the micro task message queue are not the same queue
When the main thread has finished processing the tasks in the current call stack, it gives priority to reading messages from the micro-task message queue, that is, performing the callback of the micro-task first, waiting for the micro-task's message queue to be empty, and then reading the macro task's message queue.
Pay attention to distinguish between push order and read order.
Therefore, tasks in js are divided into synchronous tasks and asynchronous tasks.
Synchronization tasks refer to the tasks that the current main thread is about to digest, which together form the execution stack (executioncontextstack).
An asynchronous task is a task that is not entered into the main thread, but into the task queue (taskqueque), that is, a task that will not be carried out immediately.
The answer to the question about how javascript is asynchronous is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.