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

这三个概念有什么不同?


当前回答

每个人都很难将异步与并行或并发相关联,因为异步既不是并行的反义词,也不是并发的反义词。它是同步的反义词。它只是表示某些东西,在这个例子中是线程,是否会与其他东西同步,在这个例子中是另一个线程。

其他回答

“同步和异步是编程模型。并发和并行是任务执行的方式……” 来源:https://medium.com/better - programming/sync vs -异步并发- vs - vs - 5754 cdb60f66平行

换句话说,同步和异步描述了在进行函数调用时程序如何执行(它是等待还是继续执行?),而并发和并行描述了函数(任务)将如何执行(并发=可能同时执行,并行=有效地同时执行)。

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

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.

这里有一些语义需要澄清:

并发或并行是资源争用的问题,而异步是关于控制流的问题。

不同的过程(或它们的组成操作)被称为异步的,当它们的处理顺序没有确定的实现时;换句话说,它们中的任何一个都有可能在任何给定的时间t被处理。根据定义,多个处理器(例如cpu或Persons)使多个处理器同时被处理成为可能;在单个处理器上,它们的处理是交错的(例如线程)。

当异步过程或操作共享资源时,它们被称为Concurrent;当没有资源共享(例如不同的处理器和存储)时,并行性是微不足道的保证;否则必须解决并发控制问题。

因此,异步过程或操作可以与其他过程或操作并行或并发地处理。

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

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

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

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

Concurrency Concurrency means that an application is making progress on more than one task at the same time (concurrently). Well, if the computer only has one CPU the application may not make progress on more than one task at exactly the same time, but more than one task is being processed at a time inside the application. It does not completely finish one task before it begins the next. Parallelism Parallelism means that an application splits its tasks up into smaller subtasks which can be processed in parallel, for instance on multiple CPUs at the exact same time. Concurrency vs. Parallelism In Detail As you can see, concurrency is related to how an application handles multiple tasks it works on. An application may process one task at at time (sequentially) or work on multiple tasks at the same time (concurrently). Parallelism on the other hand, is related to how an application handles each individual task. An application may process the task serially from start to end, or split the task up into subtasks which can be completed in parallel. As you can see, an application can be concurrent, but not parallel. This means that it processes more than one task at the same time, but the tasks are not broken down into subtasks. An application can also be parallel but not concurrent. This means that the application only works on one task at a time, and this task is broken down into subtasks which can be processed in parallel. Additionally, an application can be neither concurrent nor parallel. This means that it works on only one task at a time, and the task is never broken down into subtasks for parallel execution. Finally, an application can also be both concurrent and parallel, in that it both works on multiple tasks at the same time, and also breaks each task down into subtasks for parallel execution. However, some of the benefits of concurrency and parallelism may be lost in this scenario, as the CPUs in the computer are already kept reasonably busy with either concurrency or parallelism alone. Combining it may lead to only a small performance gain or even performance loss. Make sure you analyze and measure before you adopt a concurrent parallel model blindly.

从http://tutorials.jenkov.com/java-concurrency/concurrency-vs-parallelism.html