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

发布于 2024-12-28 03:25:55 字数 259 浏览 0 评论 0原文

让我们来谈谈 JavaScript 代码,它每 2 秒就有一个 setInterval 方法。

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

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

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

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

I also have a onblur animation event for some control.

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

Question:
Does async programming mean multi-threading? (in any way?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

另类 2025-01-04 03:25:55

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

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

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

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

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

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

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.

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.

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.

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.

孤者何惧 2025-01-04 03:25:55

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

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

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

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

如果您有一个 sleep 调用,您可以只执行 sleep 直到下一个 doLater 处理程序,这比您取消安排单个处理程序后的繁忙等待更有效线程并让操作系统做其他事情。

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.

For those interested, here is a naive (and horribly inefficient toy) implementation of a single threaded event loop in 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.

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.

魂ガ小子 2025-01-04 03:25:55
  1. 没有异步编程并不意味着专门的多线程。

  2. 为了同时实现多个任务,我们在 Node js 中使用多线程和事件循环架构。

  3. JavaScript 是同步且单线程的,这就是为什么 Node js 也是单线程的,但使用事件循环 Node 执行非阻塞 I/O 操作。 Node.js 使用 libuv 库,该库使用固定大小的线程池来处理并行任务的执行。

线程是一系列代码指令,也是进程的子单元。

在多线程中,进程有多个线程。
在单线程中,进程具有单线程。

  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.

  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. Node.js uses the libuv library that uses a fixed-sized thread pool that handles the execution of parallel tasks.

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.

破晓 2025-01-04 03:25:55

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

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

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/

风和你 2025-01-04 03:25:55

我真的建议你阅读这两篇文章,它们完全解释了异步任务如何在 js 中运行。有承诺地处理工作或使用网络工作人员之间有什么区别?

https://blog.devgenius.io/the-difference-异步与线程之间45235ebe253d
https://blog.devgenius.io/async-vs- threading-javascript-web-workers-cecb0115d097

总之,异步任务不在单独的线程中运行,它们在主线程中运行,但事件循环处理此异步机制,如果你想体验js中的多线程,可以使用webworkers。

I really suggest you to read these 2 articles that completely explain how async tasks run in js. and what are the differences between handling job with promise or using web workers.

https://blog.devgenius.io/the-difference-between-async-and-threading-45235ebe253d
https://blog.devgenius.io/async-vs-threading-javascript-web-workers-cecb0115d097

in summary async tasks not running in separate thread they are running in main thread but event loop handle this async mechanism, if you want to experience multi-threading in js you can using web workers.

顾冷 2025-01-04 03:25:55

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文