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

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

问题描述

让我们谈谈每 2 秒具有 setInterval 方法的 JavaScript 代码.

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.

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

我知道 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 的数量和操作系统的支持,这可能是真的,也可能是由复杂的调度算法造成的错觉).出于这个原因,多线程环境很困难,并且涉及线程锁定彼此的内存以防止它们彼此溢出的问题.

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 中,当人们愚蠢地在服务器中进行大量繁重的数学运算时 - 最好将其推入一个小服务器,该节点然后等待"提供答案.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 和Chrome 有这些此脚本已停止响应"中断处理程序.

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天全站免登陆