并发是让两个任务在不同的线程上并行运行。然而,异步方法在同一个线程上并行运行。这是如何实现的?还有,并行性呢?
这三个概念有什么不同?
并发是让两个任务在不同的线程上并行运行。然而,异步方法在同一个线程上并行运行。这是如何实现的?还有,并行性呢?
这三个概念有什么不同?
当前回答
总结
并发(Concurrent):多件事情似乎同时发生(具有在并发任务之间快速切换的能力;它们是并发的,因为每个任务都想要一块资源,CPU等等。)
并行是指同时发生多件事情(执行线程的数量与执行内核的数量密切相关)
异步是简单的非阻塞,当我们必须等待的事情不会让我们忙于等待(需要某种通知机制从我们离开的地方继续)
其他回答
并发和并行实际上是相同的原理,两者都与同时执行的任务有关,尽管我想说并行任务应该是真正的多任务,“同时”执行,而并发可能意味着任务共享执行线程,同时仍然看起来是并行执行。
异步方法与前两个概念没有直接关系,异步被用来呈现并发或并行任务的印象,但实际上异步方法调用通常用于需要在当前应用程序之外执行工作的进程,我们不希望等待和阻塞应用程序等待响应。
例如,从数据库获取数据可能需要时间,但我们不想阻塞UI等待数据。异步调用接受回调引用,并在请求被放置到远程系统后立即将执行返回给您的代码。当远程系统执行所需的任何处理时,UI可以继续响应用户,一旦它将数据返回给回调方法,那么该方法就可以适当地更新UI(或移交更新)。
从用户的角度来看,它看起来像多任务处理,但它可能不是。
EDIT
可能值得补充的是,在许多实现中,异步方法调用会导致线程启动,但这不是必要的,这实际上取决于正在执行的操作以及如何将响应通知回系统。
当一个经理有几个员工,并且可以给他们每个人一个单独的任务时,并行性就会发生。工人们做他们的工作,并向经理提供结果。如果任务不能完全分离,例如任务之间的结果有一定的依赖性,或者需要在没有其他推理的情况下使用相同的资源,那么并行度就受到这些约束,不能完全实现。
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;当没有资源共享(例如不同的处理器和存储)时,并行性是微不足道的保证;否则必须解决并发控制问题。
因此,异步过程或操作可以与其他过程或操作并行或并发地处理。
我会让它简短而有趣,让你们能够理解这些概念。
并发与并行——任务执行的方式。
Take an example in real life: There’s a challenge that requires you to both eat a whole huge cake and sing a whole song. You’ll win if you’re the fastest who sings the whole song and finishes the cake. So the rule is that you sing and eat concurrently. How you do that does not belong to the rule. You can eat the whole cake, then sing the whole song, or you can eat half a cake, then sing half a song, then do that again, etc. Parallelism is a specific kind of concurrency where tasks are really executed simultaneously. In computer science, parallelism can only be achieved in multicore environments.
同步与异步——编程模型。
在同步中,您将代码编写为按顺序从上到下执行的步骤 底部。在异步编程模型中,你将代码作为任务编写, 然后并发执行。并发执行意味着 所有的任务都可能同时执行。
我用真实的场景来解释3个话题 假设你想去艾哈迈达巴德到孟买旅行,但你不知道怎么走,所以你决定使用地图应用程序(谷歌Maps)。
很正常但效率很低的一种方法是,你可以在开车前看完整的路径,然后你开始开车并到达目的地。
平行-你可以不断地驾驶和观察路径。 异步-你的朋友和你在车里,你给他你的手机打开地图应用程序,告诉他看地图和指导你。 同时行驶——你开了几公里,把车停在一边,看地图,找方向,然后继续开车。