并发是让两个任务在不同的线程上并行运行。然而,异步方法在同一个线程上并行运行。这是如何实现的?还有,并行性呢?

这三个概念有什么不同?


当前回答

当一个经理有几个员工,并且可以给他们每个人一个单独的任务时,并行性就会发生。工人们做他们的工作,并向经理提供结果。如果任务不能完全分离,例如任务之间的结果有一定的依赖性,或者需要在没有其他推理的情况下使用相同的资源,那么并行度就受到这些约束,不能完全实现。

Concurrency happens when a manager has several tasks but only less workers, hence some workers are given more than one task. Any worker given multiple tasks, divides each original given task to several steps and does the steps interleaved, each task result will be given back to manager as soon as every steps of it finished. Manager receive a task result while other tasks started and progressed several steps but have not finished yet. If any worker with multiple task decides not to start a single step of a given task before finishing every steps of an already started task, this is called sequentiality.

Asynchrony is any of the two above mixed or separated, seen from the manager's point of view. When the manager assigns the tasks to either few or enough workers he shall not be awaited stalled until any results are given back. He can do his personal jobs or whatever, while jobs are progressing. Usually workers do not decide how tasks should be divided into steps. An inversion of control means manager decides over steps and gives single steps to workers. So when he receives a step result from a worker, give him another step, maybe from another task. The whom under control is responsible for composing back step results into task results as well. So Asynchronicity comes with responsibility for control and probably coordination. If any worker is urged to work sequentially, from manager's point of view he is a synchronous worker.

Summary As it's simple to guess, full parallelism is an unrealisable idea unless otherwise in rare mostly trivial cases. Since reality comes with interdependent tasks and shared resources and lack of workers. So concurrency is the reality. From manager's point of view this concurrency is best if it does not hinder him from fine controlling the tasks, and if positive it is called asynchronous. Also computer software engineering best practices, augmented by S in SOLID principle, historically made servers single step runners called micro-services, this returned back control to the clients. So current situation is concurrency from server point of view and asynchronicity from client point of view.

其他回答

并发性可能发生在以下几种场景中:

异步——这意味着你的程序执行非阻塞操作。例如,它可以通过HTTP发起对远程资源的请求,然后在等待接收响应的同时继续执行一些其他任务。这有点像你发了一封电子邮件,然后继续你的生活,没有等待回复。

并行性——这意味着您的程序利用多核机器的硬件,通过将工作分解为任务,在同一时间执行任务,每个任务都在单独的核心上执行。这有点像洗澡时唱歌:你实际上是在同时做两件事。

多线程——这是一种允许不同线程并发执行的软件实现。多线程程序似乎同时在做几件事,即使它运行在单核机器上。这有点像通过各种IM窗口与不同的人聊天;虽然你实际上是在来回切换,但最终结果是你同时进行了多个对话。

Parallel : It's a broad term that means that two pieces of code execute that "at the same time". It doesn't matter if it's "real" parallelism or if it's faked through some clever design pattern. The point is that you can start the "tasks" at the same time and then control them separately (with mutex and all the appropriate tricks). But usually you prefer to use the word "parallel" only for "true" parallelism, as in : you make it happen through non-cooperative multitasking (whether be throuch CPU/GPU cores, or only at software level by letting the OS managing it at a very low level). People are reluctant to say "parallel" just for complicated sequential code that fakes parallelism, like you would find in a browser window's javascript for example. Hence the reason why people in this thread say "asynchronous has nothing to do with parallelism". Well it does, but just don't confuse them.

并发:没有并行性就不可能有并发性(无论是模拟的还是真实的,正如我上面解释的那样),但是这个术语特别关注的是两个系统将试图在某个时间点同时访问同一资源的事实。它强调了一个事实,那就是你必须要处理这个问题。

异步:每个人都说异步与并行无关,这是对的,但它为并行铺平了道路(让事情并行或不并行的负担在你身上——继续阅读)。

“异步”指的是并行性的一种表示形式,它形式化了并行性中通常涉及的三个基本内容:1)定义任务的初始化(比如它何时开始以及它获得哪些参数),2)任务完成后必须做什么,3)代码在此期间应该继续做什么。

但它仍然只是语法(通常表示为回调方法)。在后台,底层系统可能简单地认为这些所谓的“任务”只是堆积起来的代码片段,直到它完成当前正在执行的代码。然后它一个接一个地把它们拆开,然后依次执行。与否。它还可以为每个任务创建一个线程,并并行运行它们。谁在乎呢?这部分不包括在概念中;)

并发性意味着同时执行多个任务,但不一定是同时执行。当你必须执行多个任务,但你只有一个资源时,我们就会追求并发性。在单核环境中,并发是通过上下文切换实现的。

并行性就像同时执行多个任务,就像你可以一边唱歌一边洗澡。现在你在并行地做这些任务。

术语异步与线程执行有关。在异步模型中,当执行一个任务时,您可以切换到另一个任务,而无需等待前一个任务完成。

异步编程帮助我们实现并发性。多线程环境中的异步编程是实现并行的一种方法。

总结

并发(Concurrent):多件事情似乎同时发生(具有在并发任务之间快速切换的能力;它们是并发的,因为每个任务都想要一块资源,CPU等等。)

并行是指同时发生多件事情(执行线程的数量与执行内核的数量密切相关)

异步是简单的非阻塞,当我们必须等待的事情不会让我们忙于等待(需要某种通知机制从我们离开的地方继续)

我用真实的场景来解释3个话题 假设你想去艾哈迈达巴德到孟买旅行,但你不知道怎么走,所以你决定使用地图应用程序(谷歌Maps)。

很正常但效率很低的一种方法是,你可以在开车前看完整的路径,然后你开始开车并到达目的地。

平行-你可以不断地驾驶和观察路径。 异步-你的朋友和你在车里,你给他你的手机打开地图应用程序,告诉他看地图和指导你。 同时行驶——你开了几公里,把车停在一边,看地图,找方向,然后继续开车。