简体   繁体   English

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

[英]Does async programming mean multi-threading?

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 (+ 动画)的情况下,我可能会得到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...?所以...?

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

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 中,当人们愚蠢地在服务器中进行大量繁重的数学运算时 - 最好将其推入一个小服务器,该节点然后“等待”提供答案。 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 是一个很棒的小型事件交换机,但是任何需要超过 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 都有这些“此脚本已停止响应”中断处理程序。

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 .这里的概念是将doLater回调处理程序附加到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.然后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.如果您有一个sleep调用,您可以一直sleep直到下一个doLater处理程序,这比繁忙的等待更有效,因为您取消了单线程的调度并让操作系统执行其他操作。

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/

  1. No asynchronous programming doesn't mean multithreading specifically.没有异步编程并不特别意味着多线程。

  2. For achieving multiple tasks at the same time we use multi-threading and other is event loop architecture in node js.为了同时实现多个任务,我们使用多线程,其他是 node js 中的事件循环架构。

  3. JavaScript is synchronous and single threaded and that is why node js is also single threaded but with event loop node do a non-blocking I/O operations. JavaScript 是同步的和单线程的,这就是为什么 node js 也是单线程的,但使用事件循环 node 执行非阻塞 I/O 操作。 Node.js uses the libuv library that uses a fixed-sized thread pool that handles the execution of parallel tasks. Node.js 使用 libuv 库,该库使用固定大小的线程池来处理并行任务的执行。

Thread is a sequence of code instructions and also the sub unit of process.线程是一系列代码指令,也是进程的子单元。

In multi-threading, processes have multiple threads.在多线程中,进程有多个线程。 In single threading, processes have single thread.在单线程中,进程具有单线程。

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.执行单位是线程,所以,是的,其他一些线程必须直接调用回调,或者通过将一些异步过程调用排队到启动线程来调用回调。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM