from the design of Javascript, it is a single-threaded non-blocking scripting language, how to understand it?
from the design of Javascript, it is a single-threaded non-blocking scripting language, how to understand it?
just like the bank queue, there is only one window, that is, single thread, to serve one by one. When someone comes out because of a temporary delay, he or she will come out and continue the queue after finishing the work, so it is not blocked
this problem can be divided into two parts:
javascript
the runtime contains only one message queue , where each message corresponds to an associated function. This queue follows the FIFO (first in first out) mechanism, and always starts processing from the first incoming message. When processing:
when making an IO or network request, it is usually done through the event and the callback mechanism . For example, when sending a json
asynchronous data request, it will think that the function has been executed and will continue to get the next message from the message queue for processing. When the network request returns, a new message is crammed into the queue and continues to wait for processing.
but there are exceptions, such as alert
dialog box or Synchronize XHR
. If the processing is not completed, the message queue will be blocked all the time. Of course, unless there are special occasions, we should try to avoid this situation .
as we all know, javascript has been a single-threaded, non-blocking scripting language since its birth. This is determined by its original use: interacting with browsers.
single threading means that at any time the javascript code is executed, there is only one main thread to handle all tasks.
non-blocking means that when the contemporary code needs to perform an asynchronous task (a task that cannot return the result immediately and takes a certain amount of time to return, such as the Iripo event), the main thread will suspend the (pending) task, and then execute the corresponding callback according to certain rules when the asynchronous task returns the result.
single threading is necessary and the cornerstone of the javascript language, one of the reasons why we need to do a variety of dom operations in its original and primary execution environment, the browser. Imagine if javascript is multithreaded, what happens when two threads perform an operation on the dom at the same time, such as one adding an event to it and the other deleting the dom,? Therefore, to ensure that a scenario similar to that in this example does not occur, javascript chooses to use only one main thread to execute the code, thus ensuring the consistency of program execution.
of course, nowadays people also realize that single thread not only ensures the order of execution but also limits the efficiency of javascript, so web worker technology has been developed. This technology claims to make javascript a multithreaded language.
however, multithreading using web worker technology has many limitations, for example, all new threads are under the full control of the main thread and cannot be executed independently. This means that these "threads" should actually belong to the children of the main thread. In addition, these child threads do not have permission to perform the Icano operation, so they can only share some tasks such as computation for the main thread. So strictly speaking, these threads do not have full functionality, so this technology does not change the single-threaded nature of the javascript language.
predictably, javascript will always be a single-threaded language in the future.
you just need to understand single thread. Non-blocking means that it is asynchronous. In fact, it is realized by multithreading or multi-processes at the bottom of the host environment. No matter how to implement it, there is only one thread you can play with. It is designed like this in design, but there are various ways to play in implementation, so it is emphasized that in terms of design
Previous: Perform a clear timer operation, unable to clear the js timer
Next: How does the proxy of nodeJS act as an agent for file upload?
We have encountered the following problems in the distribution system! For example, A user shares his URL. B users come according to the URL shared by A users, so judge whether A users follow the official account! If there is no attention, the follow but...
recently, our company s e-commerce platform cooperated with Shentong Express to develop the electronic face order function under express delivery and query express information through Shentong Express interface? But I went to Shentong open platform to s...
the H5 side sends a request and then cannot get the data. I looked at the request in the background dispath log and found that the request had been received, the data had been queried, and the code ran out. And I didn t report it wrong. and I can re...
axios sets headers and serializes the parameters with the qs module, but the backend receive parameter is empty cannot receive time parameter ...
<select name="PROVINCE" id="PROVINCE" onchange="change2(this.value)"> 0 <option value="">------< option> < select> ------------------------------------------------------ function chan...
after the user logs in, the token sent from the backend is stored locally (localStrong, cookie or memory) assuming that when A gets the token, of B, A with the token of B can pretend to be B. is that so? ...
now APP, which has made a news, has a video column, just like the video column in Tencent News s APP, but a video may be dozens or hundreds of megabytes in size, and your server will be full soon. It is not realistic for a small APP to keep expanding it...
recently working on an h5 website, customers want to achieve the kind of background where the effect can be seen when editing. really can t find a plug-in that can preview in real time. Please give me some advice. Thank you. something like this. on...
1: the development environment is javaweb, such as oa project (spring boot, html5, jquery). 2: deploy the oa project to the production environment tomcat (non-war, non-jar). 3: when the developer updates java, html, js or resource files, how can the u...
recently, when I was studying uploading and downloading files, I didn t quite understand the upload principle of angular-file-upload.js. Is there a great god who can guide the upload principle of angular-file-upload? If it is a picture, it can be under...
Vue, Webpack, data-driven, front-end rendering, is very good. but now the problem is, the cost of SSR server rendering is very high, what if you don t have any SEO to do a news station? is there a tool for component development based on page Page?...
...
download.js is used in vue to download all png files downloaded normally by Google, but ie and Google downloaded files do not have a suffix and cannot be opened directly methods: { load() { eslint-disable-next-line no...
I don t know why the picture that starts playing will be very small. If you cancel the full screen after the full screen, the picture will be full of div boxes ...
I don t know why the picture that starts playing will be very small. If you cancel the full screen after the full screen, the picture will be full of div boxes ...
my project is to upload pictures using H5, which is mainly used on mobile devices. I use Input file camera to call a mobile phone camera to take photos and upload them. The problem now is that there is a certain probability that black images will appear...
...