multithreading 并发、并行和异步方法之间有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4844637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between concurrency, parallelism and asynchronous methods?
提问by GurdeepS
Concurrency is having two tasks run in parallel on separate threads. However, asynchronous methods run in parallel but on the same 1 thread. How is this achieved? Also, what about parallelism?
并发是让两个任务在不同的线程上并行运行。但是,异步方法并行运行但在同一个线程上。这是如何实现的?另外,并行性怎么样?
What are the differences between these 3 concepts?
这3个概念有什么区别?
采纳答案by Lazarus
Concurrent and parallel are effectively the same principle as you correctly surmise, both are related to tasks being executed simultaneously although I would say that parallel tasks should be truly multitasking, executed "at the same time" whereas concurrent could mean that the tasks are sharing the execution thread while still appearing to be executing in parallel.
并发和并行实际上与您正确推测的原理相同,两者都与同时执行的任务有关,尽管我会说并行任务应该是真正的多任务处理,“同时”执行,而并发可能意味着任务共享执行线程,同时似乎仍在并行执行。
Asynchronous methods aren't directly related to the previous two concepts, asynchrony is used to present the impression of concurrent or parallel tasking but effectively an asynchronous method call is normally used for a process that needs to do work away from the current application and we don't want to wait and block our application awaiting the response.
异步方法与前两个概念没有直接关系,异步用于呈现并发或并行任务的印象,但实际上异步方法调用通常用于需要在当前应用程序之外工作的进程,我们不这样做不想等待并阻止我们的应用程序等待响应。
For example, getting data from a database could take time but we don't want to block our UI waiting for the data. The async call takes a call-back reference and returns execution back to your code as soon as the request has been placed with the remote system. Your UI can continue to respond to the user while the remote system does whatever processing is required, once it returns the data to your call-back method then that method can update the UI (or handoff that update) as appropriate.
例如,从数据库获取数据可能需要一些时间,但我们不想阻止 UI 等待数据。异步调用采用回调引用,并在向远程系统发出请求后立即将执行返回给您的代码。当远程系统执行任何需要的处理时,您的 UI 可以继续响应用户,一旦它将数据返回到您的回调方法,则该方法可以适当地更新 UI(或移交该更新)。
From the User perspective, it appears like multitasking but it may not be.
从用户的角度来看,它看起来像多任务处理,但也可能不是。
EDIT
编辑
It's probably worth adding that in many implementations an asynchronous method call will cause a thread to be spun up but it's not essential, it really depends on the operation being executed and how the response can be notified back to the system.
可能值得补充的是,在许多实现中,异步方法调用会导致线程启动,但这不是必需的,它实际上取决于正在执行的操作以及如何将响应通知回系统。
回答by Vipin Jain
In Short,
简而言之,
Concurrency means multiple tasks which start, run, and complete in overlapping time periods, in no specific order. Parallelism is when multiple tasks OR several part of a unique task literally run at the same time, e.g. on a multi-core processor.
并发意味着多个任务在重叠的时间段内开始、运行和完成,没有特定的顺序。并行是指多个任务或一个独特任务的几个部分实际上是同时运行的,例如在多核处理器上。
Remember that Concurrency and parallelism are NOT the same thing.
请记住,并发性和并行性不是一回事。
Differences between concurrency vs. parallelism
并发与并行之间的差异
Now let's list down remarkable differences between concurrency and parallelism.
现在让我们列出并发和并行之间的显着差异。
Concurrency is when two tasks can start, run, and complete in overlapping time periods. Parallelism is when tasks literally run at the same time, eg. on a multi-core processor.
并发是指两个任务可以在重叠的时间段内启动、运行和完成。并行是指任务实际上是同时运行的,例如。在多核处理器上。
Concurrency is the composition of independently executing processes, while parallelism is the simultaneous execution of (possibly related) computations.
并发是独立执行进程的组合,而并行是同时执行(可能相关的)计算。
Concurrency is about dealing with lots of things at once. Parallelism is about doing lots of things at once.
并发是关于同时处理很多事情。并行是关于同时做很多事情。
An application can be concurrent – but not parallel, which means that it processes more than one task at the same time, but no two tasks are executing at same time instant.
一个应用程序可以是并发的——但不是并行的,这意味着它同时处理多个任务,但没有两个任务同时执行。
An application can be parallel – but not concurrent, which means that it processes multiple sub-tasks of a task in multi-core CPU at same time.
一个应用程序可以是并行的——但不能是并发的,这意味着它在多核 CPU 中同时处理一个任务的多个子任务。
An application can be neither parallel – nor concurrent, which means that it processes all tasks one at a time, sequentially.
应用程序既不能并行,也不能并发,这意味着它一次一个地按顺序处理所有任务。
An application can be both parallel – and concurrent, which means that it processes multiple tasks concurrently in multi-core CPU at same time.
一个应用程序既可以是并行的,也可以是并发的,这意味着它可以在多核 CPU 中同时处理多个任务。
Concurrency
并发
Concurrency is essentially applicable when we talk about minimum two tasks or more. When an application is capable of executing two tasks virtually at same time, we call it concurrent application. Though here tasks run looks like simultaneously, but essentially they MAY not. They take advantage of CPU time-slicing feature of operating system where each task run part of its task and then go to waiting state. When first task is in waiting state, CPU is assigned to second task to complete it's part of task.
Operating system based on priority of tasks, thus, assigns CPU and other computing resources e.g. memory; turn by turn to all tasks and give them chance to complete. To end user, it seems that all tasks are running in parallel. This is called concurrency.
当我们谈论最少两个或更多任务时,并发本质上是适用的。当一个应用程序能够虚拟地同时执行两个任务时,我们称之为并发应用程序。虽然这里的任务运行看起来像同时运行,但本质上它们可能不是。它们利用操作系统的 CPU 时间切片特性,其中每个任务运行其任务的一部分,然后进入等待状态。当第一个任务处于等待状态时,CPU 被分配给第二个任务来完成它的一部分任务。
操作系统基于任务的优先级,从而分配 CPU 和其他计算资源,例如内存;轮流完成所有任务并给他们完成的机会。对于最终用户来说,似乎所有任务都是并行运行的。这称为并发。
Parallelism
并行性
Parallelism does not require two tasks to exist. It literally physically run parts of tasks OR multiple tasks, at the same time using multi-core infrastructure of CPU, by assigning one core to each task or sub-task.
Parallelism requires hardware with multiple processing units, essentially. In single core CPU, you may get concurrency but NOT parallelism.
并行性不需要存在两个任务。通过为每个任务或子任务分配一个核心,它实际上在物理上运行部分任务或多个任务,同时使用 CPU 的多核基础设施。
本质上,并行需要具有多个处理单元的硬件。在单核 CPU 中,您可能会获得并发性,但不会获得并行性。
Asynchronous methods
异步方法
This is not related to Concurrency and parallelism, asynchrony is used to present the impression of concurrent or parallel tasking but effectively an asynchronous method call is normally used for a process that needs to do work away from the current application and we don't want to wait and block our application awaiting the response.
这与并发和并行无关,异步用于呈现并发或并行任务的印象,但实际上,异步方法调用通常用于需要在当前应用程序之外工作的进程,而我们不想这样做等待并阻止我们的应用程序等待响应。
回答by Dimos
Concurrencyis when the execution of multiple tasks is interleaved, instead of each task being executed sequentially one after another.
并发是指多个任务的执行交错进行,而不是每个任务一个接一个地依次执行。
Parallelismis when these tasks are actually being executed in parallel.
并行是指这些任务实际上是并行执行的。
Asynchronyis a separate concept (even though related in some contexts). It refers to the fact that one event might be happening at a different time (not in synchrony) to another event. The below diagrams illustrate what's the difference between a synchronous and an asynchronous execution, where the actors can correspond to different threads, processes or even servers.
异步是一个单独的概念(即使在某些上下文中相关)。它指的是一个事件可能发生在与另一个事件不同的时间(不同步)这一事实。下图说明了同步和异步执行之间的区别,其中参与者可以对应于不同的线程、进程甚至服务器。
回答by Aloysius Snuffleupagus
Everyone is having trouble associating asynchronous to either parallelism or concurrency because asynchronous is not an antonym to either parallel or concurrent. It is an antonym of Synchronous. Which just indicates if something, in this case threads, will be synched with something else, in this case another thread.
每个人都很难将异步与并行或并发相关联,因为异步不是并行或并发的反义词。它是同步的反义词。这只是表明某些东西(在这种情况下是线程)是否会与其他东西(在这种情况下是另一个线程)同步。
回答by rahulaga_dev
There are several scenarios in which concurrency can occur:
有几种可能发生并发的场景:
Asynchrony— This means that your program performs non-blocking operations. For example, it can initiate a request for a remote resource via HTTP and then go on to do some other task while it waits for the response to be received. It's a bit like when you send an email and then go on with your life without waiting for a response.
异步——这意味着你的程序执行非阻塞操作。例如,它可以通过 HTTP 发起对远程资源的请求,然后在等待接收响应的同时继续执行其他一些任务。这有点像当您发送电子邮件然后继续您的生活而无需等待回复。
Parallelism— This means that your program leverages the hardware of multi-core machines to execute tasks at the same time by breaking up work into tasks, each of which is executed on a separate core. It's a bit like singing in the shower: you're actually doing two things at exactly the same time.
并行性——这意味着您的程序利用多核机器的硬件通过将工作分解为任务来同时执行任务,每个任务都在单独的内核上执行。这有点像在淋浴时唱歌:你实际上同时在做两件事。
Multithreading— This is a software implementation allowing different threads to be executed concurrently. A multithreaded program appears to be doing several things at the same time even when it's running on a single-core machine. This is a bit like chatting with different people through various IM windows; although you're actually switching back and forth, the net result is that you're having multiple conversations at the same time.
多线程——这是一种软件实现,允许同时执行不同的线程。即使在单核机器上运行时,多线程程序似乎也在同时做几件事。这有点像通过各种IM窗口和不同的人聊天;尽管您实际上是来回切换,但最终结果是您同时进行了多个对话。
回答by jeancallisti
Parallel :It's a broad term that means that two pieces of code execute that "at the same time" to the point where the parallel execution becomes "real". Sounds vague and simplistic? Yes. I'm trying to help you focus on the differencesbetween those concepts rather than providing each individual technical definition.
并行:这是一个广义的术语,意味着两段代码“同时”执行到并行执行变得“真实”的程度。听起来模糊和简单?是的。我试图帮助您关注这些概念之间的差异,而不是提供每个单独的技术定义。
So I wrote "real" because parallelism can be simulated to a certain degree. Many systems, for example games, implement "parallel" subsystems that perform lots of task during each execution loop (for example: some of them make extensive use of agents), but most of the time they're only parallel in the sense that they each do their little thing in no predictibleorder, access data seemingly randomly, and even if you implemented some sort of primitive software-based cooperativemultitasking to organize the whole thing, it's still not reallyparallel. It's just a very complicated sequential system.
所以我写了“real”,因为在一定程度上可以模拟并行性。许多系统,例如游戏,实现了在每个执行循环期间执行大量任务的“并行”子系统(例如:其中一些广泛使用代理),但大多数时候它们只是从某种意义上说它们是并行的每个人都以不可预测的顺序做他们的小事,看似随机地访问数据,即使你实现了某种基于软件的原始协作多任务来组织整个事情,它仍然不是真正的并行。它只是一个非常复杂的顺序系统。
You might say that parallelism becomes real when there's a third-party system (whether be an underlying preemptive OS offering threads, or CPU cores) specifically designed to run code in black boxes that you can't control (except for the time at which they start and the result they produce, plus any mutex or semaphores you might throw in)
您可能会说,当有第三方系统(无论是提供线程的底层抢占式操作系统,还是 CPU 内核)专门设计用于在您无法控制的黑匣子中运行代码时(除了它们的时间),并行性才成为现实。开始和他们产生的结果,加上你可能投入的任何互斥体或信号量)
Concurrent :there can't be concurrency without parallelism (whether simulated or real, as I explained above), but this term focuses specifically on the fact that the two systems will try to access the same resourceat the same time at some point. It puts the emphasis on the fact that you're going to have to deal with that.
并发:没有并行性(无论是模拟的还是真实的,正如我上面解释的那样),就不可能有并发,但是这个术语特别关注这样一个事实,即两个系统将在某个时间点同时尝试访问相同的资源。它强调了你将不得不处理的事实。
Asynchronous: everyone is right by saying that asynchronous is unrelated with parallelism, but it paves the way to it (the burden is on you to make things parallel or not -- keep reading).
异步:每个人都说异步与并行无关,这是正确的,但它为它铺平了道路(你的负担是让事情并行与否——继续阅读)。
You might see this concept as a way to representparallelism by formalizing the three basic things usually involved in parallelism : 1) define the task's initialization (say when it starts and what parameters it gets), 2) what must be done after it finishes and 3) What the code should continue doing inbetween.
您可能会将这个概念视为通过形式化通常涉及并行性的三个基本事项来表示并行性的一种方式:1) 定义任务的初始化(比如它何时开始以及它获得什么参数),2) 完成后必须做什么以及3)代码应该在中间继续做什么。
But it's still only syntax (usually it's represented as callback methods). Behind the scene, the underlying system might simply decide that these so-called "tasks" are just fragments of code to pile up until it finishes the code it's currently executing. And then it unpiles them one by one and executes them sequentially. Or not. It might also create a thread per task and run them in parallel. Who cares? That part is not included in the concept ;)
但它仍然只是语法(通常它表示为回调方法)。在幕后,底层系统可能只是简单地认为这些所谓的“任务”只是代码片段,在它完成当前正在执行的代码之前要堆积起来。然后它一个一个地拆开它们并依次执行它们。或不。它还可能为每个任务创建一个线程并并行运行它们。谁在乎?该部分不包含在概念中;)
回答by LONGHORN007
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.
并发
并发意味着应用程序同时(同时)在多个任务上取得进展。好吧,如果计算机只有一个 CPU,应用程序可能不会同时处理多个任务,但在应用程序内部一次处理多个任务。在开始下一项任务之前,它不会完全完成一项任务。
并行性
并行意味着应用程序将其任务拆分为更小的子任务,这些子任务可以并行处理,例如在多个 CPU 上同时处理。
并发与并行详解
如您所见,并发性与应用程序如何处理它所处理的多个任务有关。应用程序可以一次(顺序)处理一项任务,也可以同时(并发)处理多项任务。
另一方面,并行性与应用程序如何处理每个单独的任务有关。应用程序可以从头到尾串行处理任务,或者将任务拆分为可以并行完成的子任务。
如您所见,应用程序可以并发,但不能并行。这意味着它同时处理多个任务,但这些任务不会分解为子任务。
应用程序也可以是并行的,但不能是并发的。这意味着应用程序一次只能处理一个任务,并且该任务被分解为可以并行处理的子任务。
此外,应用程序既不能并发也不能并行。这意味着它一次只处理一个任务,并且该任务永远不会分解为并行执行的子任务。
最后,应用程序也可以是并发和并行的,因为它既可以同时处理多个任务,也可以将每个任务分解为子任务以并行执行。然而,在这种情况下,并发和并行的一些好处可能会失去,因为计算机中的 CPU 已经合理地忙于并发或并行。结合它可能只会导致很小的性能提升甚至性能损失。在盲目采用并发并行模型之前,请务必进行分析和测量。
From http://tutorials.jenkov.com/java-concurrency/concurrency-vs-parallelism.html
来自http://tutorials.jenkov.com/java-concurrency/concurrency-vs-parallelism.html
回答by Evans AB
There's a bit of semantics to clear up here:
这里有一些语义需要澄清:
Concurrency or Parallelism is a question of resource contention, whereas Asynchronous is about control flow.
并发或并行是资源争用的问题,而异步是关于控制流的。
Different procedures(or their constituent operations) are termed Asynchronous, when there's no deterministic implementation of the the order of their processing; in other words, there's a probability that any of them could be processed at any given time T. By definition, multiple processors (e.g. CPUs or Persons) make it possible for several of them to be processed at the same time; on a single processor, their processing is interleaved (e.g. Threads).
当它们的处理顺序没有确定性的实现时,不同的过程(或它们的组成操作)被称为异步;换句话说,它们中的任何一个都有可能在任何给定的时间 T 被处理。根据定义,多个处理器(例如 CPU 或人员)可以同时处理其中的几个;在单个处理器上,它们的处理是交错的(例如线程)。
Asynchronous procedures or operations are termed Concurrent, when they share resources; Concurrency is the definite possibility of contention at any given time T. Parallelism is trivially guaranteed when no resources are shared (e.g. different processor and storage); otherwise Concurrency control must be addressed.
异步过程或操作在共享资源时称为并发;并发性是在任何给定时间 T 发生争用的确定可能性。当没有资源共享时(例如不同的处理器和存储),并行性是微不足道的保证;否则必须解决并发控制问题。
Hence an Asynchronous procedure or operation may be processed in Parallel or Concurrently with others.
因此,异步过程或操作可以与其他过程或操作并行或并发处理。
回答by Pedro Boechat
"Sync and async are programming models. Concurrent and parallel are ways tasks are executed...". Source: https://medium.com/better-programming/sync-vs-async-vs-concurrent-vs-parallel-5754cdb60f66
“同步和异步是编程模型。并发和并行是执行任务的方式......”。来源:https: //medium.com/better-programming/sync-vs-async-vs-concurrent-vs-parallel-5754cdb60f66
回答by Dhirendra Gautam
Concurrency means executing multiple tasks at the same time but not necessarily simultaneously. When you have to perform more than one task but you have a single resource then we go for concurrency. In single core environment concurrency is achieved by Context Switching.
并发意味着同时执行多个任务,但不一定同时执行。当您必须执行多项任务但只有一个资源时,我们会选择并发。在单核环境中,并发是通过上下文切换来实现的。
Parallelism is like performing more than one task simultaneously like you can sing and bath together. Now you are doing task in Parallel.
并行就像同时执行多项任务,就像您可以一起唱歌和洗澡一样。现在您正在并行执行任务。
Asynchronous is something that is related to thread execution in asynchronous model when one task gets executed, you could switch to a different task without waiting for the previous to get completed.
异步是与异步模型中的线程执行相关的事情,当一个任务被执行时,您可以切换到另一个任务而无需等待前一个任务完成。
Asynchronous programming helps us to achieve concurrency. Asynchronous programming in a multi-threaded environment is a way to achieve parallelism.
异步编程帮助我们实现并发。多线程环境中的异步编程是实现并行性的一种方式。