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

这三个概念有什么不同?


当前回答

并发+并行都意味着同时运行多个任务。我看到一些人认为这是有区别的,但这取决于你咨询的参考文献,没有真正的正确或错误的答案。

Asynchronous: In some communities this means non-blocking code, which can mean two things: It almost always means it will not block an OS thread. However, non-blocking can optionally mean that the next line of source code in a function will continue to run without delay. In Python await asyncio.sleep(5) blocks execution of the function, but not the OS thread, and that is considered async. In Golang, you have "goroutines" that similarly to Python's await, they block execution of code, but not OS threads, however, this is not referred to as async in the Golang community. It's just concurrent programming.

其他回答

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

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

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

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

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

并发和并行实际上是相同的原理,两者都与同时执行的任务有关,尽管我想说并行任务应该是真正的多任务,“同时”执行,而并发可能意味着任务共享执行线程,同时仍然看起来是并行执行。

异步方法与前两个概念没有直接关系,异步被用来呈现并发或并行任务的印象,但实际上异步方法调用通常用于需要在当前应用程序之外执行工作的进程,我们不希望等待和阻塞应用程序等待响应。

例如,从数据库获取数据可能需要时间,但我们不想阻塞UI等待数据。异步调用接受回调引用,并在请求被放置到远程系统后立即将执行返回给您的代码。当远程系统执行所需的任何处理时,UI可以继续响应用户,一旦它将数据返回给回调方法,那么该方法就可以适当地更新UI(或移交更新)。

从用户的角度来看,它看起来像多任务处理,但它可能不是。


EDIT

可能值得补充的是,在许多实现中,异步方法调用会导致线程启动,但这不是必要的,这实际上取决于正在执行的操作以及如何将响应通知回系统。

并发+并行都意味着同时运行多个任务。我看到一些人认为这是有区别的,但这取决于你咨询的参考文献,没有真正的正确或错误的答案。

Asynchronous: In some communities this means non-blocking code, which can mean two things: It almost always means it will not block an OS thread. However, non-blocking can optionally mean that the next line of source code in a function will continue to run without delay. In Python await asyncio.sleep(5) blocks execution of the function, but not the OS thread, and that is considered async. In Golang, you have "goroutines" that similarly to Python's await, they block execution of code, but not OS threads, however, this is not referred to as async in the Golang community. It's just concurrent programming.

总结

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

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

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