异步编程意味着多线程吗?

时间:2022-09-23 21:00:16

lets talk about JavaScript code which has setInterval methods every 2 sec.

让我们谈谈每2秒就有setInterval方法的JavaScript代码。

I also have a onblur animation event for some control.

我也有一个onblur动画事件的一些控制。

In a case where onblur occurs (+ animation), I might get the setInterval function.

在出现onblur (+ animation)的情况下,我可能会获得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...?

所以…?

4 个解决方案

#1


64  

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 wait until 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.

因此,事件驱动的异步编程避免了传统多线程编程的许多缺陷,比如内存争用问题。可能仍然存在竞态条件,因为事件处理的顺序不是由您决定的,但是它们很少见,而且更容易管理。另一方面,由于事件处理程序在当前运行的函数到达空闲点之前不会交付事件,所以有些函数可能会使编程的其余部分中断。这个发生在节点。例如,当人们愚蠢地在服务器上做大量繁重的计算时——最好将其放入一个小服务器中,节点会“等待”以交付答案。节点。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都有“这个脚本已经停止响应”中断处理程序。

#2


5  

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

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

The concept here is that you attach doLater callback handlers to the eventLoop. Then the eventLoop is just a while(true) that checks whether the specific timestamp for each doLater handler 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 doLater callbacks.

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

If you have a sleep call you could just do sleep until the next doLater handler which is more efficient then a busy wait since you deschedule your single thread and let the OS do other things.

如果你有一个睡眠电话,你可以一直睡到下一个更有效率的处理器,这样你就会忙着等待,因为你会把你的单线程处理掉,让操作系统做其他事情。

#3


0  

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/

#4


-3  

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.

如果有回叫,就必须调用它。执行单元是线程&因此,是的,其他一些线程必须调用回调,或者直接调用,或者通过对发起线程的异步过程调用进行排队。

#1


64  

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 wait until 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.

因此,事件驱动的异步编程避免了传统多线程编程的许多缺陷,比如内存争用问题。可能仍然存在竞态条件,因为事件处理的顺序不是由您决定的,但是它们很少见,而且更容易管理。另一方面,由于事件处理程序在当前运行的函数到达空闲点之前不会交付事件,所以有些函数可能会使编程的其余部分中断。这个发生在节点。例如,当人们愚蠢地在服务器上做大量繁重的计算时——最好将其放入一个小服务器中,节点会“等待”以交付答案。节点。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都有“这个脚本已经停止响应”中断处理程序。

#2


5  

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

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

The concept here is that you attach doLater callback handlers to the eventLoop. Then the eventLoop is just a while(true) that checks whether the specific timestamp for each doLater handler 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 doLater callbacks.

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

If you have a sleep call you could just do sleep until the next doLater handler which is more efficient then a busy wait since you deschedule your single thread and let the OS do other things.

如果你有一个睡眠电话,你可以一直睡到下一个更有效率的处理器,这样你就会忙着等待,因为你会把你的单线程处理掉,让操作系统做其他事情。

#3


0  

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/

#4


-3  

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.

如果有回叫,就必须调用它。执行单元是线程&因此,是的,其他一些线程必须调用回调,或者直接调用,或者通过对发起线程的异步过程调用进行排队。