Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Is ajax multithreaded?

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "ajax is not multithreaded", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "ajax is not multithreaded" this article.

Ajax is not multithreaded, but single-threaded, because JavaScript is single-threaded, and Ajax belongs to the category of JavaScript, so Ajax is single-threaded. The reason why you can use Ajax to achieve multithreading depends on the Web server, and the server uses multithreading to process requests.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Is Ajax single threaded or multithreaded?

First of all, let's be clear: Ajax is single-threaded, because JavaScript is single-threaded and Ajax belongs to the category of JavaScript, so Ajax is single-threaded. The reason why we can use Ajax to achieve multithreading depends on the Web server, and the server uses multithreading to process requests.

Then be clear: browsers are multi-process. As shown in the figure, every time a google browser opens a tab tab, there is a child process.

Because JS runs in the browser, it is single-threaded, and each window has a JS thread. Since it is single-threaded, only specific code can be executed at a particular time and block other code.

Ajax async?

Ajax requests are indeed asynchronous, and the js engine executes asynchronous code without waiting because there are message queues and event loops, and the request is made by a new thread by the browser. Many behaviors in the browser are asynchronous. When an asynchronous event occurs, it enters the event queue. The browser has an internal large message loop, Event Loop (event loop), which polls the large event queue and handles the event. Only after the previous processing is finished, the event will be executed idle, while the JavaScript engine always runs the callback function in a single thread.

Conclusion:

The Ajax request is indeed asynchronous, it is a new thread request by the browser, and the event callback is put into the Event loop single-threaded event queue for processing.

In the message queue is the callback function that we added when we registered the asynchronous task, and the main thread continues to execute other code after initiating the AJAX request. The AJAX thread is responsible for requesting the url. When it gets the response, it encapsulates the response into a JavaScript object and then constructs a message:

/ / ajax request $.ajax (url, function (resp) {console.log ('I am the response:', resp);}); / / the message in the message queue looks like this: var message = function () {callbackFn (response);} / where callbackFn is the callback function when the response is successful in the previous code.

After the main thread executes all the code in the current loop, it goes to the message queue to fetch the message and execute it. At this point, the worker thread's notification to the main thread is completed, and the callback function is executed. If the main thread does not provide a callback function in the first place, there is no need for the AJAX thread to notify the main thread after receiving the HTTP response, thus there is no need to put messages on the message queue. Among them, the callback function of the asynchronous process must not be executed in the current event loop.

These are all the contents of the article "is ajax multithreaded or not". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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: 265

*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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report