事件循环,回调队列和Javascript的单线程如何连接? [英] How are the Event Loop, Callback Queue, and Javascript’s single thread connected?

查看:214
本文介绍了事件循环,回调队列和Javascript的单线程如何连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道以下javascript环境如何作为系统互连.

I’d like to know how the following pieces of a javascript environment interconnect as a system.

  • JavaScript引擎
  • 事件循环
  • 事件队列

由于节点已在另一篇文章中介绍(

We can limit this to a browser environment since node has been covered in another article (here)

  • Javascript是单线程的,因此只有一个调用堆栈.

  • Javascript is single threaded and therefore only has one callstack.

Javascript环境仅提供了一些真正异步的功能.这些可能包括setTimeout(),setInterval()和I/O函数.

Javascript environments provide only a few functions that are truly asynchronous. These may include setTimeout(), setInterval(), and I/O function(s).

      console.log(‘Sync code started…’);

      setTimeout(function asyncLog() {
           console.log(‘Async function has completed’)
      }, 2000);

      console.log(‘Sync code finished…')

示例步骤:

(如果我输入错了,请更正步骤)

EXAMPLE STEPS:

( Please correct steps if I’m wrong )

  1. 同步代码已开始..."已记录
  2. setTimeout已添加到堆栈中,但立即返回控件
  3. setTimeout是否发送到其他线程"…工人"?在javascript的单线程之外,以计算2000毫秒
  4. 同步代码已完成..."已记录
  5. 在2000毫秒后,asyncLog()被推送到事件队列
  6. 因为清除了调用堆栈,所以事件循环会检查事件队列中是否有待处理的回调
  7. asyncLog()从队列中删除,并由事件循环推入堆栈
  8. 已记录异步功能已完成"
  9. 呼叫堆栈现在很清楚

问题

如果有人可以大致了解异步函数(如setTimeout)从第一次到达调用堆栈到调用它们的时间,以及如何进行这些操作的步骤,则无需一一回答.调用栈.

QUESTIONS

These don’t need to be answered one by one if someone could produce an overview of the steps of how and where async functions (such as setTimeout) go from the time they first hit the callstack to when they are called back to the callstack.

  1. 在第3步中,谁产生了这个新线程?是浏览器吗?
    • 这个新线程被正确阻止了吗?
    • 如果您的循环创建了1000个setTimeouts,会发生什么情况.是否创建了1000个线程"?
    • 一次可以产生多少个线程是否有限制?
    • 新线程完成执行后,如何终止在队列中?
  1. On step 3, who produces this new thread? Is it the browser?
    • This new thread is being blocked correct?
    • What happens if you have a loop that creates 1000 setTimeouts. Are 1000 ‘threads’ created?
    • Is there a limit to how many threads can be spawned at a time?
    • When new thread finishes executing, how does it end up on the queue?
  • 事件循环是否轮询事件队列?
  • JavaScript的线程是否知道事件循环?还是事件循环只是将事物压入堆栈?
  • 事件循环如何知道何时清除堆栈?

推荐答案

您的理解和您的示例似乎基本上是正确的.现在,向您提出问题:

Your understanding and your example seem to be basically correct. Now, to your questions:

在第3步中,谁产生了这个新线程?是浏览器吗?

On step 3, who produces this new thread? Is it the browser?

是的.基本上是为那些真正异步"功能提供实现的东西. IIRC,setTimeout是直接在JS引擎中实现的,而联网IO绝对是浏览器的责任-但创建它们的人并不重要.最后,在浏览器环境"中,它始终是浏览器的一部分.

Yes. It is basically the thing that supplies the implementation for those "truly asynchronous" functions. IIRC, setTimeout is implemented in JS engines directly, while networking IO would be definitely the browser's responsibility - but it doesn't really matter who creates them. In the end, in your "browser environment" it's always some part of the browser.

这个新线程被正确阻止了吗?

This new thread is being blocked correct?

是的.否.这取决于需要完成的工作,即您调用了哪个异步函数.有些可能需要旋转新线程,但是对于简单的超时,我很确定使用了非阻塞系统调用.

Yes. No. It depends on the work that needs to be done, i.e. which async function you called. Some may require spinning of a new thread, but for simple timeouts I'm pretty sure that a non-blocking system call is used.

如果您的循环创建了1000个setTimeouts,会发生什么情况.是否创建了1000个线程"?

What happens if you have a loop that creates 1000 setTimeouts. Are 1000 ‘threads’ created?

可能.不过,不太可能.我假设对于那些确实需要自己的线程的异步操作,使用了线程池,并对请求进行了排队.该池的大小可能隐藏在浏览器配置的肠子中.

Possible. Unlikely, though. I'd assume for those async actions that really require their own thread, a thread pool is used, and requests are queued. The size of this pool might be hidden in the bowels of your browser's configuration.

一次可以产生多少个线程是否有限制?

Is there a limit to how many threads can be spawned at a time?

这将由操作系统控制.

当新线程完成执行时,它如何在队列中结束?谁提供事件队列?

When new thread finishes executing, how does it end up on the queue? Who supplies the Event Queue?

基本上,每个这样的线程的最后一个动作是将其结果放入事件队列中.

Basically, the last action of each such thread is to put its result in the event queue.

谁提供事件循环?事件循环会轮询事件队列吗?

Who supplies the Event Loop? Does the event loop poll the Event Queue?

我想说的是这是一个实现细节,无论是循环轮询队列还是队列驱动循环迭代.

I'd say that's an implementation detail, whether the loop polls the queue or the queue drives the loop iterations.

javascript的线程是否知道事件循环?还是事件循环只是将事物压入堆栈?

Is the javascript’s thread aware of an event loop? Or does the Event loop just push things onto the stack?

我会说javascript在事件循环线程中运行.事件循环只是反复从队列中弹出事件并执行其javascript.

I'd say that the javascript runs in the event loop thread. The event loop just repeatedly pops events from the queue and executes their javascript.

事件循环如何知道何时清除堆栈?

How does the Event loop know when the stack is clear?

事件循环调用 javascript执行-因此,当javascript返回时,堆栈是透明的.

The event loop calls the javascript execution - so the stack is clear when the javascript returns.

这篇关于事件循环,回调队列和Javascript的单线程如何连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆