multithreading 如何阐明异步和并行编程之间的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6133574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 01:14:46  来源:igfitidea点击:

How to articulate the difference between asynchronous and parallel programming?

multithreadingasynchronousparallel-processing

提问by Matt Sherman

Many platforms promote asynchrony and parallelism as means for improving responsiveness. I understand the difference generally, but often find it difficult to articulate in my own mind, as well as for others.

许多平台将异步和并行作为提高响应能力的手段。我大体上理解其中的差异,但经常发现很难在我自己以及其他人的脑海中表达出来。

I am a workaday programmer and use async & callbacks fairly often. Parallelism feels exotic.

我是一名日常程序员,经常使用异步和回调。并行性给人一种异国情调的感觉。

But I feel like they are easily conflated, especially at the language design level. Would love a clear description of how they relate (or don't), and the classes of programs where each is best applied.

但我觉得它们很容易混为一谈,尤其是在语言设计层面。希望清楚地描述它们如何相关(或不相关),以及最适合应用的程序类别。

回答by CodeMan

When you run something asynchronously it means it is non-blocking, you execute it without waiting for it to complete and carry on with other things. Parallelism means to run multiple things at the same time, in parallel. Parallelism works well when you can separate tasks into independent pieces of work.

当您异步运行某些东西时,这意味着它是非阻塞的,您无需等待它完成即可执行它并继续执行其他操作。并行意味着同时并行运行多个事物。当您可以将任务分成独立的工作部分时,并行性工作得很好。

Take for example rendering frames of a 3D animation. To render the animation takes a long time so if you were to launch that render from within your animation editing software you would make sure it was running asynchronouslyso it didn't lock up your UI and you could continue doing other things. Now, each frame of that animation can also be considered as an individual task. If we have multiple CPUs/Cores or multiple machines available, we can render multiple frames in parallelto speed up the overall workload.

以渲染 3D 动画的帧为例。渲染动画需要很长时间,因此如果您要从动画编辑软件中启动该渲染,您将确保它异步运行,因此它不会锁定您的 UI,并且您可以继续做其他事情。现在,该动画的每一帧也可以被视为一项单独的任务。如果我们有多个 CPU/内核或多台机器可用,我们可以并行渲染多个帧以加快整体工作负载。

回答by igon

I believe the main distinction is between concurrencyand parallelism.

我相信主要区别在于concurrencyparallelism之间。

Asyncand Callbacksare generally a way (tool or mechanism) to express concurrency i.e. a set of entities possibly talking to each other and sharing resources. In the case of async or callback communication is implicit while sharing of resources is optional (consider RMI where results are computed in a remote machine). As correctly noted this is usually done with responsiveness in mind; to not wait for long latencyevents.

异步回调通常是一种表达并发性的方式(工具或机制),即一组可能相互交谈并共享资源的实体。在异步或回调通信的情况下是隐式的,而资源共享是可选的(考虑在远程机器中计算结果的 RMI)。正如正确指出的那样,这通常是在考虑响应性的情况下完成的;不等待长延迟事件。

Parallel programming has usually throughput as the main objective while latency, i.e. the completion time for a single element, might be worse than a equivalent sequential program.

并行编程通常以吞吐量为主要目标,而延迟,即单个元素的完成时间,可能比等效的顺序程序更糟糕。

To better understand the distinction between concurrency and parallelism I am going to quote from Probabilistic models for concurrencyof Daniele Varacca which is a good set of notes for theory of concurrency:

为了更好地理解并发和并行之间的区别,我将引用Daniele Varacca 的并发概率模型,这是一组很好的并发理论笔记:

A model of computation is a model for concurrency when it is able to represent systems as composed of independent autonomous components, possibly communicating with each other. The notion of concurrency should not be confused with the notion of parallelism. Parallel computations usually involve a central control which distributes the work among several processors. In concurrency we stress the independence of the components, and the fact that they communicate with each other.Parallelism is like ancient Egypt, where the Pharaoh decides and the slaves work. Concurrency is like modern Italy, where everybody does what they want, and all use mobile phones.

当计算模型能够将系统表示为由独立的自治组件组成并可能相互通信时,它就是并发模型。并发的概念不应与并行的概念混淆。并行计算通常涉及在多个处理器之间分配工作的中央控制。在并发中,我们强调组件的独立性,以及它们相互通信的事实。并行性就像古埃及,法老决定,奴隶工作。并发就像现代意大利,每个人都可以随心所欲,并且都使用手机。

In conclusion, parallel programming is somewhat a special case of concurrency where separate entities collaborate to obtain high performance and throughput (generally).

总之,并行编程在某种程度上是并发的一种特殊情况,其中单独的实体协作以获得高性能和吞吐量(通常)。

Async and Callbacks are just a mechanism that allows the programmer to express concurrency. Consider that well-known parallel programming design patterns such as master/worker or map/reduce are implemented by frameworks that use such lower level mechanisms (async) to implement more complex centralizedinteractions.

异步和回调只是一种允许程序员表达并发的机制。考虑到众所周知的并行编程设计模式,例如 master/worker 或 map/reduce,是由框架实现的,这些框架使用此类较低级别的机制(异步)来实现更复杂的集中式交互。

回答by Paul

This article explains it very well: http://urda.cc/blog/2010/10/04/asynchronous-versus-parallel-programming

这篇文章解释得很好:http: //urda.cc/blog/2010/10/04/asynchronous-versus-parallel-programming

It has this about asynchronous programming:

它有关于异步编程的内容:

Asynchronous calls are used to prevent “blocking” within an application. [Such a] call will spin-off in an already existing thread (such as an I/O thread) and do its task when it can.

异步调用用于防止应用程序中的“阻塞”。[此类] 调用将在已经存在的线程(例如 I/O 线程)中分拆,并在可能的情况下执行其任务。

this about parallel programming:

这是关于并行编程:

In parallel programming you still break up work or tasks, but the key differences is that you spin up new threads for each chunk of work

在并行编程中,您仍然会分解工作或任务,但关键区别在于您为每个工作块启动新线程

and this in summary:

总结一下:

asynchronous callswill use threads already in use by the systemand parallel programmingrequires the developer to break the work up, spinup, and teardown threads needed.

异步调用将使用系统已在使用的线程并行编程要求开发人员分解所需的工作、启动和拆除线程

回答by Andrew Cooper

My basic understanding is:

我的基本理解是:

Asynchonous programming solves the problem of waiting around for an expensive operation to complete before you can do anything else. If you can get other stuff done while you're waiting for the operation to complete then that's a good thing. Example: keeping a UI running while you go and retrieve more data from a web service.

异步编程解决了在您可以做任何其他事情之前等待昂贵的操作完成的问题。如果您可以在等待操作完成的同时完成其他工作,那么这是一件好事。示例:在您前往并从 Web 服务检索更多数据时保持 UI 运行。

Parallel programming is related but is more concerned with breaking a large task into smaller chunks that can be computed at the same time. The results of the smaller chunks can then be combined to produce the overall result. Example: ray-tracing where the colour of individual pixels is essentially independent.

并行编程是相关的,但更关注将大型任务分解为可以同时计算的较小块。然后可以组合较小块的结果以产生整体结果。示例:光线追踪,其中单个像素的颜色基本上是独立的。

It's probably more complicated than that, but I think that's the basic distinction.

它可能比这更复杂,但我认为这是基本的区别。

回答by Leonard H. Martin

I tend to think of the difference in these terms:

我倾向于考虑这些术语的区别:

Asynchronous: Go away and do this task, when you're finished come back and tell me and bring the results. I'll be getting on with other things in the mean time.

异步:出去做这个任务,完成后回来告诉我并带来结果。与此同时,我会处理其他事情。

Parallel: I want you to do this task. If it makes it easier, get some folks in to help. This is urgent though, so I'll wait here until you come back with the results. I can do nothing else until you come back.

平行:我要你做这个任务。如果这样更容易,请一些人帮忙。不过这很紧急,所以我会在这里等你,直到你带着结果回来。在你回来之前,我无能为力。

Of course an asynchronous task might make use of parallelism, but the differentiation - to my mind at least - is whether you get on with other things while the operation is being carried out or if you stop everything completely until the results are in.

当然,异步任务可能会利用并行性,但区别 - 至少在我看来 - 是您在执行操作时是否继续处理其他事情,或者您是否完全停止一切直到结果出现。

回答by serkan

async: Do this by yourselfsomewhere else and notify me when you complete(callback). By the time i can continue to do my thing.

async自己在其他地方执行此操作,并在完成时通知我(回调)。到时候我可以继续做我的事情。

enter image description here

enter image description here

parallel: Hire as many guys(threads) as you wishand split the job to them to complete quickerand let me know(callback) when you complete. By the time i mightcontinue to do my other stuff.

并行:根据需要雇用尽可能多的人(线程)并将工作分配给他们以更快地完成并在完成后通知我(回调)。到时候我可能会继续做我的其他事情。

enter image description here

enter image description here

the main difference is parallelism mostly depends on hardware.

主要区别在于并行性主要取决于硬件。

回答by Richard

It is a question of order of execution.

这是执行顺序的问题。

If A is asynchronous with B, then I cannot predict beforehand when subparts of A will happen with respect to subparts of B.

如果 A 与 B 异步,那么我无法预先预测 A 的子部分何时会相对于 B 的子部分发生。

If A is parallel with B, then things in A are happening at the same time as things in B. However, an order of execution may still be defined.

如果 A 与 B 并行,则 A 中的事情与 B 中的事情同时发生。但是,仍然可以定义执行顺序。

Perhaps the difficulty is that the word asynchronous is equivocal.

也许困难在于异步这个词是模棱两可的。

I execute an asynchronous task when I tell my butler to run to the store for more wine and cheese, and then forget about him and work on my novel until he knocks on the study door again. Parallelism is happening here, but the butler and I are engaged in fundamentally different tasks and of different social classes, so we don't apply that label here.

当我告诉我的管家跑到商店买更多的酒和奶酪时,我执行了一个异步任务,然后忘记他并继续写我的小说,直到他再次敲门。并行性在这里发生,但管家和我从事根本不同的任务,属于不同的社会阶层,所以我们不在这里贴上这个标签。

My team of maids is working in parallel when each of them is washing a different window.

当她们每个人都在清洗不同的窗户时,我的女佣团队正在并行工作。

My race car support team is asynchronously parallel in that each team works on a different tire and they don't need to communicate with each other or manage shared resources while they do their job.

我的赛车支持团队是异步并行的,因为每个团队在不同的轮胎上工作,他们在工作时不需要相互沟通或管理共享资源。

My football (aka soccer) team does parallel work as each player independently processes information about the field and moves about on it, but they are not fully asynchronous because they must communicate and respond to the communication of others.

我的足球(又名足球)队进行并行工作,因为每个球员都独立处理有关场地的信息并在场地上走动,但他们并非完全异步,因为他们必须与他人进行交流并做出回应。

My marching band is also parallel as each player reads music and controls their instrument, but they are highly synchronous: they play and march in time to each other.

我的军乐队也是平行的,因为每个演奏者都在阅读音乐并控制他们的乐器,但他们是高度同步的:他们演奏并按时进行。

A cammed gatling gun could be considered parallel, but everything is 100% synchronous, so it is as though one process is moving forward.

凸轮加特林机枪可以被认为是并行的,但一切都是 100% 同步的,因此就好像一个过程正在向前推进。

回答by Code-EZ

Why Asynchronous ?

为什么是异步?

With today's application's growing more and more connected and also potentially long running tasks or blocking operations such as Network I/O or Database Operations.So it's very important to hide the latency of these operations by starting them in background and returning back to the user interface quickly as possible. Here Asynchronous come in to the picture, Responsiveness.

随着当今应用程序的连接越来越多,并且可能会长时间运行任务或阻塞操作,例如网络 I/O 或数据库操作。因此,通过在后台启动这些操作并返回到用户界面来隐藏这些操作的延迟非常重要尽快。这里 Asynchronous 出现在图片中,Responsiveness

Why parallel programming?

为什么要并行编程?

With today's data sets growing larger and computations growing more complex. So it's very important to reduce the execution time of these CPU-bound operations, in this case, by dividing the workload into chunks and then executing those chunks simultaneously. We can call this as "Parallel" . Obviously it will give high Performanceto our application.

随着今天的数据集越来越大,计算越来越复杂。因此,减少这些受 CPU 限制的操作的执行时间非常重要,在这种情况下,将工作负载划分为多个块,然后同时执行这些块。我们可以称之为“并行”。显然,它将为我们的应用程序提供高性能

回答by Varun Shridhar

AsynchronousLet's say you are the point of contact for your client and you need to be responsive i.e. you need to share status, complexity of operation, resources required etc whenever asked. Now you have a time-consuming operation to be done and hence cannot take this up as you need to be responsive to the client 24/7. Hence, you delegate the time-consuming operation to someone else so that you can be responsive. This is asynchronous.

异步假设您是客户的联系人,您需要做出响应,即您需要在任何时候共享状态、操作的复杂性、所需的资源等。现在您需要完成一项耗时的操作,因此无法接受,因为您需要 24/7 全天候响应客户。因此,您将耗时的操作委托给其他人,以便您能够做出响应。这是异步的。

Parallel programmingLet's say you have a task to read, say, 100 lines from a text file, and reading one line takes 1 second. Hence, you'll require 100 seconds to read the text file. Now you're worried that the client must wait for 100 seconds for the operation to finish. Hence you create 9 more clones and make each of them read 10 lines from the text file. Now the time taken is only 10 seconds to read 100 lines. Hence you have better performance.

并行编程假设您有一项任务要从一个文本文件中读取 100 行,而读取一行需要 1 秒。因此,您需要 100 秒来阅读文本文件。现在您担心客户端必须等待 100 秒才能完成操作。因此,您又创建了 9 个克隆,并使每个克隆从文本文件中读取 10 行。现在阅读 100 行所需的时间仅为 10 秒。因此你有更好的表现。

To sum up, asynchronous coding is done to achieve responsiveness and parallel programming is done for performance.

综上所述,异步编码是为了实现响应性,并行编程是为了性能。

回答by Aditya Bokade

Asynchronous: Running a method or task in background, without blocking. May not necessorily run on a separate thread. Uses Context Switching / time scheduling.

异步:在后台运行方法或任务,无阻塞。不一定要在单独的线程上运行。使用上下文切换/时间调度。

Parallel Tasks: Each task runs parallally. Does not use context switching / time scheduling.

并行任务:每个任务并行运行。不使用上下文切换/时间调度。