In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "JavaScript single thread and task queue use case analysis". In daily operation, I believe that many people have doubts about JavaScript single thread and task queue use case analysis. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful to answer the doubts of "JavaScript single thread and task queue use case analysis". Next, please follow the editor to study!
Why is JavaScript designed to be single-threaded?
One of the major features of the JavaScript language is single threading, in other words, only one thing can be done at a time.
For (var j = 0; j < 5; jacks +) {console.log (j);} console.log ('end')
In the above code, the end will not be executed until the for loop is finished.
The single thread of JavaScript is related to its purpose. As a browser scripting language, the main purpose of JavaScript is to interact with users and to manipulate DOM. This determines that it can only be single-threaded, otherwise it will bring very complex synchronization problems.
Suppose JavaScript has two threads at the same time, one thread adds content to a DOM node, and the other thread deletes the node, which thread should the browser follow?
So, to avoid complexity, JavaScript has been single-threaded since its birth, which has become a core feature of the language and will not change in the future.
In order to take advantage of the computing power of multicore CPU, HTML5 proposes the Web Worker standard, which allows JavaScript scripts to create multiple threads, but child threads are completely controlled by the main thread and cannot operate DOM. Therefore, this new standard does not change the nature of JavaScript single-threading.
II. Task queue
A "task queue" is a queue of events (which can also be understood as a queue of messages). When an IO device completes a task, it adds an event to the "task queue" to indicate that the related asynchronous tasks can enter the "execution stack". The main thread reads the "task queue", which is to read what events are in it.
Let's explain:
Javascript is single-threaded, which means that all tasks need to be queued and the previous task ends before the next task is executed. If the former task takes a long time, the latter task will have to wait all the time.
If the queue is due to a large amount of computation, it is understandable that CPU takes up too much, but very often, CPU is idle or there is a lot of space unused, because IO devices (input and output devices) are very slow (for example, Ajax operations read data from the network), and you can only wait for the results before you can proceed.
The designers of the JavaScript language realized that the main thread could completely ignore the IO device, suspend these waiting tasks, and run the next tasks first. Wait until the IO device returns the result before performing the pending task.
Therefore, all tasks can be divided into two types, one is synchronous task (synchronous), the other is asynchronous task (asynchronous).
A synchronous task means that a task queued on the main thread can only be executed after the previous task has been executed.
An asynchronous task refers to a task that does not enter the main thread but enters the "task queue" (task queue). Only when the "task queue" notifies the main thread that an asynchronous task can be executed.
Specifically, the operating mechanism of asynchronous execution is as follows. The same is true of synchronous execution, because it can be seen as asynchronous execution without asynchronous tasks. )
All synchronization tasks are executed on the main thread, forming an execution stack
Besides the main thread, there is also a "task queue" (task queue). As long as the asynchronous task has a run result, place an event in the Task queue.
Once all the synchronization tasks in the execution stack are completed, the system reads the Task queue to see what events are in it. Those corresponding asynchronous tasks then end the waiting state, enter the execution stack, and begin execution.
The main thread repeats the third step above.
For (var j = 0; j < 5; jacks +) {console.log (j); setTimeout (function () {console.log ('timeout');}, 0);} console.log (' end')
/ / results 0 1 2 3 4 end timeout timeout timeout timeout timeout
At this point, the study on "JavaScript single thread and task queue usage case analysis" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.