How does a single thread handle asynchronous code in JavaScript?

news/2024/7/7 18:42:45 标签: javascript, runtime

原文: https://www.quora.com/How-does-a-single-thread-handle-asynchronous-code-in-JavaScript

 

--------------------------------------------------------------------------------

Well, arguably its not true that Javascript is single threaded if you see from the under hood working of browser JS to your JS code, there are thread pools. By single threaded what they mean(browser end) is your JS runs into a single threaded event loop. There is one single thread that handles your event loop. Under your JS, the browser code is running multiple threads to capture events and trigger handlers, when they capture any new event, they push it on an event queue and then that event loop, in which your code is running gets triggered and it handles the request e.g. It performs an action which can be to show a DIV, which again triggers the Browser to print it, which in turn runs a thread to do it(from the thread pool).

Lets take an example of your JS algo.
window.onload 
     Show Header
     Send an Ajax Req. for config - > When done, alert a box
     Do some more page animations
So when you told the browser to send an ajax request with a provided callback, it saves it in memory and this Network IO call is transferred to a thread in Network Pool Threads, your code next to that line will continue to work. After the Network Call thread has done its job, it will push on the event queue the response. Remember that event loop? Here is when it comes to action, it picks the top entity and then trigger your callback(Context Switching on CPU Level), which in turn could be anything. Now try doing something like this.
window.onload
     Show Header
     Send an Ajax req. for config -> 
     When done -> Trigger another Ajax 
         -> for loop 0.100000000
     Do more animation

Now here, if even your second Ajax completes in due time, its callback will not be triggered until your for loop exits. Its a sort of blocking within the thread, none of the event will be fired and everything will be frozen!

Hope that clears your doubt.

Cheers!

-----------------------------------------------------------------------------------------------------------------------

Javscript is single-threaded. Each browser window has only one Javascript thread running inside them. What makes the asynchronous events possible is the browser’s Event Loop and the associated Event Queue.

Suppose Javascript engine is running some function, and in the meantime, user clicks on a button on the webpage. A Key Press event would be fired and since Javascript engine is busy doing some other work, this event would be queued in the Event Queue.

Javascript handling the Event Queue would look something like

  1. while (waitForMessage()) {
  2. processMessage();
  3. }

waitForMessage() waits for a message synchronously in the queue and when it gets one, it processes that message.

In the example above, after Javascript engine finishes executing that function, it checks for the next message in the queue. It sees that some Key Press event was fired while it was busy doing some other work. So, it handles that Key Press event either by calling any callbacks that may be bind to that key press event or doing some thing else.

But all of this happens so fast, that it feels like every thing is running parallely. But in reality, all of this is just a simple queue managed by a single thread running a infinite loop.

For information about how threads work in general , you can checkout this article.

Threading Explained

----------------------------------------------------------------------------------------------------------

You can look at the presentation at Introduction to Node js.
Though it is in context of NodeJS but the basics remain the same.
The JS runtime i.e. V8 is same in case of browser (Chrome) and Node.

Slide 12 talks about Single Threaded nature.
Slide 13-16 talk about other properties of JS like non-blocking, event driven etc.
Image on Slide 17 talks about the event loop Dron Rathore is talking about.
Analogy of working style of JS with the real word 'King and Servants' on slide 18 helps a lot understanding it as a whole.

Hope this helps.

-----------------------------------------------------------------------------------------------------------

You may have noticed that in JavaScript code every function you write returns without waiting. You never write JavaScript code that looks like this:

  1. while (true) {
  2. API.blockingCall()
  3. }

One consequence of this is that all the functions you write return to their caller as soon as they have done their thing. All the functions that call functions you write do the same thing. Ultimately, they return to the dispatcher of the single-threaded event loop. The dispatcher dequeues the next event from the event queue, identifies the function to call to handle that event and the process continues - no function ever blocks, which means every function returns to its caller in a short period of time.

So, this is how your call back function gets invoked. The function you wrote that registered the callback - and the the functions that called it - return to their caller.  Then, and only then, and only if the AJAX request is complete or has failed, does the function which implements your callback get called, but only when the dispatcher in the single-threaded event loop reaches the event that will cause that function to be invoked.

There is no magic here - no interrupting code to service a response. It is just the event loop dispatcher spinning through its queue of events, one at a time, calling functions on a single thread.

To make your mental picture complete, you need to imagine these unseen things:

* a queue of unprocessed events

* a loop that iterates through the queue of unprocessed events one at a time, and invokes the event handler function for those events.

* a mechanism that allows the result of an AJAX call to be enqueued on the queue of unprocessed events.

转载于:https://www.cnblogs.com/oxspirt/p/9087366.html


http://www.niftyadmin.cn/n/1051014.html

相关文章

java数据库设计中的14个技巧(zhuang SCDN)

下述十四个技巧,是许多人在大量的数据库分析与设计实践中,逐步总结出来的。对于这些经验的运用,读者不能生帮硬套,死记硬背,而要消化理解,实事求是,灵活掌握。并逐步做到:在应用中发…

SpringBoot入门建站全系列(五)使用Spring-data-jpa操作数据库CRUD

SpringBoot入门建站全系列(五)使用Spring-data-jpa操作数据库 SpringBoot操作数据库有多种方式,如 JDBC直接操作:太古老了,没人愿意这样玩 Mybatis插件:比较时髦,比较适合sql复杂,或者对性能…

Spring和Netty整合详解

Spring和Netty整合详解 本篇主要介绍netty如何跟Spring配合,其实真的很没必要将netty和Spring牵扯在一起,我们完全可以用netty做出一个spring的;然而在《Spring环境下使用Netty写Socket和Http详解》一篇中,因为没怎么用到Spring&…

yield from (python生成器)

#生成器中的yield from是干什么用的(一般多用于线程,协程那)def func(): # for i in AB: # yield i yield from AB # 就相当于上面的for循环,把循环简化了(后面跟可迭代对象) g func()print(list(g))转载于…

Spring和Spring Mvc 5整合详解

Spring和Spring Mvc 5整合详解 一、官方主页 Spring Spring Mvc 低版本的SpringMvc,可以参考这一篇《Spring和Spring Mvc整合详解》 二、概述 Spring Mvc的启动方式不同于Spring Boot,Spring Boot内嵌了tomcat容器,可以打包成jar文件快…

struts2文件上传相关资料收集整合

前一阵子有些朋友在电子邮件中问关于Struts 2实现文件上传的问题, 所以今天我们就来讨论一下这个问题。 实现原理Struts 2是通过Commons FileUpload文件上传。Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器将文件…

vue中detele删除对象属性时视图不能响应更新 - 解决办法

如下代码片段 // js obj {a: 123,b: 223,c: 323 }// html <li v-for"item in obj">{{ item }}</li> 此时若在methods中使用 delete this.obj.a 或者 delete this.obj["a"]会发现视图中的li不会实时更新&#xff0c;这种情况是由于原生delete并…

重温PHP之插入排序

插入排序基本思路&#xff1a;将数组分为两个区(已排序区和未排序区)&#xff0c;假定数组的第一个元素处于已排序区&#xff0c; 第一个元素之后的所有元素都处于未排序部分。排序时用到双层循环&#xff0c;外层循环用于从未排序部分中取出待排序元素&#xff0c;并逐步缩小未…