是否异步编程意味着多线程? [英] Does async programming mean multi-threading?

查看:152
本文介绍了是否异步编程意味着多线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们来谈谈JavaScript的code具有的setInterval 方法每个 2 秒。

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

我也有一些控制的onblur 动漫盛会。

I also have a onblur animation event for some control.

在其中的onblur 出现的情况下(+动画),我可能会在的setInterval 功能。

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

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

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

我知道,JavaScript是不是多线程语言。

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

所以...?

推荐答案

没有。它字面上的意思是什么是指:异步的。了解异步编程和基于线程的编程之间的差异对您的成功作为一个程序员的关键。

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.

在多线程环境中,编程的许多个体线程在同时运行。 (取决于CPU的数量和操作系统的支持,这可能是从字面上真实的,或者它可以是由复杂的调度算法创建的错觉)。由于这个原因,多线程环境是困难的,涉及螺纹锁紧彼此的存储器至$ P $的问题从超速彼此pvent它们。

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.

在一个异步的环境中,单个进程的线程中运行所有的时间,但它可为事件驱动的原因(这是关键)从一个功能到另一个开关。当一个事件发生时,的当当前运行的进程打在这一点,它必须等待另一个事件的,javascript的核心然后扫描其事件的列表,并提供下一个,在(正式)不确定的(但可能确定​​的)秩序,事件管理器。

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.

由于这个原因,事件驱动的,异步编程避免了许多传统的,多线程编程的缺陷,如内存争用问题。可能还有比赛条件,在事件的处理顺序是达不到你,但他们非常罕见,更易于管理。而另一方面,因为直到当前正在运行的功能击中当场空闲的事件处理程序不传递的事件,有些功能可以饿死编程的其余部分。这发生在Node.js的,例如,当人们愚蠢地做很多繁重的数学在server--是最好的塞进一个小的服务器节点,然后等待提供了答案。 Node.js的是事件的一个伟大的小交换机,但任何需要更长的时间超过100毫秒应该在客户端/服务器的方式来处理。

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.

在浏览器环境,DOM事件被视为自动事件点(他们必须是,修改DOM提供了大量的事件),但即使有不好定义的JavaScript可以饿死的核心,这就是为什么Firefox和铬有这些此脚本已停止响应中断处理程序。

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.

这篇关于是否异步编程意味着多线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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