javascript 异步编程是否意味着多线程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8963209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:09:39  来源:igfitidea点击:

Does async programming mean multi-threading?

javascriptjquery.netmultithreadingasynchronous

提问by Royi Namir

lets talk about JavaScript code which has setIntervalmethods every 2sec.

让我们谈谈每秒钟都有setInterval方法的JavaScript 代码2

I also have a onbluranimation event for some control.

我还有一个onblur用于某些控件的动画事件。

In a case where onbluroccurs (+ animation), I might get the setIntervalfunction.

onblur发生(+动画)的情况下,我可能会得到该setInterval功能。

So my question is:
Does Async programming mean multi-threading? (in any way?)

所以我的问题是:
异步编程是否意味着多线程?(以任何方式?)

I know that Javascript is not a multi-threading language.

我知道 Javascript 不是多线程语言。

So...?

所以...?

回答by Elf Sternberg

No. It means literally what it means-- asynchronous. Understanding the difference between asynchronous programming and thread-based programming is critical to your success as a programmer.

不。它的字面意思是它的意思 - 异步。了解异步编程和基于线程的编程之间的区别对于您作为程序员的成功至关重要。

In a traditional, non-threaded environment, when a function must wait on an external event (such as a network event, a keyboard or mouse event, or even a clock event), the program must waituntil that event happens.

在传统的非线程环境中,当函数必须等待外部事件(例如网络事件、键盘或鼠标事件,甚至时钟事件)时,程序必须等到该事件发生。

In a multi-threaded environment, many individual threads of programming are running at the same time. (Depending upon the number of CPUs and the support of the operating system, this may be literally true, or it may be an illusion created by sophisticated scheduling algorithms). For this reason, multi-threaded environments are difficult and involve issues of threads locking each other's memory to prevent them from overrunning one another.

在多线程环境中,许多单独的编程线程同时运行。(根据 CPU 的数量和操作系统的支持,这可能是真的,也可能是由复杂的调度算法造成的错觉)。由于这个原因,多线程环境很困难,并且涉及线程锁定彼此的内存以防止它们彼此溢出的问题。

In an asychronous environment, a single process thread runs all the time, but it may, for event-driven reasons (and that is the key), switch from one function to another. When an event happens, and when the currently running process hits a point at which it must wait for another event, the javascript core then scans its list of events and delivers the next one, in a (formally) indeterminate (but probably deterministic) order, to the event manager.

在异步环境中,单个进程线程一直在运行,但出于事件驱动的原因(这是关键),它可能会从一个函数切换到另一个函数。当一个事件发生时,当当前运行的进程到达一个必须等​​待另一个事件的点时,javascript 核心然后扫描它的事件列表并以(正式)不确定(但可能是确定性的)顺序传递下一个事件, 给活动经理。

For this reason, event-driven, asynchronous programming avoids many of the pitfalls of traditional, multi-threaded programming, such as memory contention issues. There may still be race conditions, as the order in which events are handled is not up to you, but they're rare and easier to manage. On the other hand, because the event handler does not deliver events until the currently running function hits an idle spot, some functions can starve the rest of the programming. This happens in Node.js, for example, when people foolishly do lots of heavy math in the server-- that's best shoved into a little server that node then "waits" to deliver the answer. Node.js is a great little switchboard for events, but anything that takes longer than 100 milliseconds should be handled in a client/server way.

因此,事件驱动的异步编程避免了传统多线程编程的许多缺陷,例如内存争用问题。可能仍然存在竞争条件,因为处理事件的顺序不取决于您,但它们很少见且更易于管理。另一方面,由于事件处理程序在当前运行的函数到达空闲位置之前不会传递事件,因此某些函数可能会使其余的编程变得饥饿。例如,这发生在 Node.js 中,当人们愚蠢地在服务器中进行大量繁重的数学运算时——最好将其推入一个小服务器,该节点然后“等待”提供答案。Node.js 是一个很棒的小型事件交换机,但是任何需要超过 100 毫秒的时间都应该以客户端/服务器的方式处理。

In the browser environment, DOM events are treated as automatic event points (they have to be, modifying the DOM delivers a lot of events), but even there badly-written Javascript can starve the core, which is why both Firefox and Chrome have these "This script is has stopped responding" interrupt handlers.

在浏览器环境中,DOM 事件被视为自动事件点(它们必须是,修改 DOM 会传递很多事件),但即使是写得不好的 Javascript 也会使核心饿死,这就是为什么 Firefox 和 Chrome 都有这些“此脚本已停止响应”中断处理程序。

回答by Raynos

A single threaded event loop is a good example of being asynchronous in a single threaded language.

单线程事件循环是单线程语言中异步的一个很好的例子。

The concept here is that you attach doLatercallback handlers to the eventLoop. Then the eventLoopis just a while(true)that checks whether the specific timestamp for each doLaterhandler is met, and if so it calls the handler.

这里的概念是将doLater回调处理程序附加到eventLoop. 然后eventLoop它只是while(true)检查是否满足每个doLater处理程序的特定时间戳,如果满足则调用处理程序。

For those interested, here is a naive (and horribly inefficient toy) implementation of a single threaded event loop in JavaScript

对于那些感兴趣的人,这里是JavaScript 中单线程事件循环的一个幼稚(而且效率极低的玩具)实现

This does mean that without any kind of OS thread scheduler access of your single thread, your forced to busy wait on the doLatercallbacks.

这确实意味着如果没有任何类型的操作系统线程调度程序访问您的单个线程,您将被迫忙于等待doLater回调。

If you have a sleepcall you could just do sleepuntil the next doLaterhandler which is more efficient then a busy wait since you deschedule your single thread and let the OS do other things.

如果您有sleep电话,您可以sleep在下一个doLater更高效的处理程序之前执行此操作,因为您取消了单线程的调度并让操作系统执行其他操作,因此您需要进行繁忙的等待。

回答by Jeffrey Sweeney

Only in the sense that it executes code haphazardly and risks race conditions. You will not get any performance benefits from using timeouts and intervals.

只是在它随意执行代码并冒着竞争条件的风险的意义上。使用超时和间隔不会带来任何性能优势。

However, HTML5's WebWorkers do allow for real multithreading in the browser: http://www.html5rocks.com/en/tutorials/workers/basics/

然而,HTML5 的 WebWorkers 确实允许在浏览器中实现真正的多线程:http: //www.html5rocks.com/en/tutorials/workers/basics/

回答by Martin James

If there is a callback, something has to call it. The units of execution are threads & so, yes, some other thread has to call the callback, either directly or by queueing up some asynchronous procedure call to the initiating thread.

如果有回调,则必须调用它。执行单位是线程,所以,是的,其他一些线程必须直接调用回调,或者通过将一些异步过程调用排队到启动线程来调用回调。