并发是让两个任务在不同的线程上并行运行。然而,异步方法在同一个线程上并行运行。这是如何实现的?还有,并行性呢?
这三个概念有什么不同?
并发是让两个任务在不同的线程上并行运行。然而,异步方法在同一个线程上并行运行。这是如何实现的?还有,并行性呢?
这三个概念有什么不同?
当前回答
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
其他回答
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
用类比来解释这些术语。
你的房子需要洗碗和洗衣服。
并发性:你不需要等完成一个任务后再启动另一个。例如,你可以先洗碗,也可以同时洗碗。他们可以按照任何顺序完成,也就是说,即使你先洗碗,也可能会先洗衣服。
平行性:你的房子里有不止一个人在做这件事;例如,你可以洗碗,而另一个人可以洗衣服。
异步:你让别人去洗衣服,你让别人去洗碗。他们实际上可以是同一个人(也就是说,你告诉他们去洗衣服,同时也告诉他们去洗碗)。他们处理完每个人后会向你报告。
同步:你叫别人去洗碗。你等着他们。当他们完成后,你可以做其他的事情(你可以告诉他们接下来洗衣服,或者你自己洗,或者你可以完全做其他的事情;重点是你在完成第一个任务时被阻塞,你与他们是同步的)。
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)代码在此期间应该继续做什么。
但它仍然只是语法(通常表示为回调方法)。在后台,底层系统可能简单地认为这些所谓的“任务”只是堆积起来的代码片段,直到它完成当前正在执行的代码。然后它一个接一个地把它们拆开,然后依次执行。与否。它还可以为每个任务创建一个线程,并并行运行它们。谁在乎呢?这部分不包括在概念中;)
这里有一些语义需要澄清:
并发或并行是资源争用的问题,而异步是关于控制流的问题。
不同的过程(或它们的组成操作)被称为异步的,当它们的处理顺序没有确定的实现时;换句话说,它们中的任何一个都有可能在任何给定的时间t被处理。根据定义,多个处理器(例如cpu或Persons)使多个处理器同时被处理成为可能;在单个处理器上,它们的处理是交错的(例如线程)。
当异步过程或操作共享资源时,它们被称为Concurrent;当没有资源共享(例如不同的处理器和存储)时,并行性是微不足道的保证;否则必须解决并发控制问题。
因此,异步过程或操作可以与其他过程或操作并行或并发地处理。
并发+并行都意味着同时运行多个任务。我看到一些人认为这是有区别的,但这取决于你咨询的参考文献,没有真正的正确或错误的答案。
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.