What is the difference between concurrency, parallelism and asynchronous methods?
Concurrency is having two tasks run in parallel on separate threads. However, asynchronous methods run in pa开发者_C百科rallel but on the same 1 thread. How is this achieved? Also, what about parallelism?
What are the differences between these 3 concepts?
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.
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.
Concurrency is when the execution of multiple tasks is interleaved, instead of each task being executed sequentially one after another.
Parallelism is when these tasks are actually being executed in parallel.
Asynchrony is 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.
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.
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.
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.
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 a 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 the tasks in parallel.
The term asynchronous is related to thread execution. In an asynchronous model, when one task gets executed, you can switch to a different task without waiting for the previous task to get completed.
Asynchronous programming helps us to achieve concurrency. Asynchronous programming in a multi-threaded environment is a way to achieve parallelism.
"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
In other words, sync and async describe how your program executes when making a function call (will it wait or will it continue executing?), whilst concurrent and parallel describe how a function (a task) will be executed (concurrent = possibly executed at the same time, parallel = effectively executed at the same time).
I'm going to make it short and interesting to wrap your head around these concepts.
Concurrent vs. Parallel - Ways tasks are executed.
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.
Synchronous vs. Asynchronous - Programming models.
In sync, you write code as steps that are executed in order, from top to bottom. In an async programming model, you write code as tasks, which are then executed concurrently. Executing concurrently means that all the tasks are likely executed at the same time.
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.
From http://tutorials.jenkov.com/java-concurrency/concurrency-vs-parallelism.html
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).
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.
Hence an Asynchronous procedure or operation may be processed in Parallel or Concurrently with others.
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.
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 resource at 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).
"Asynchronous" refers to a representation of parallelism that formalizes 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.
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 ;)
Explaining these terms with an analogy.
Your house needs the dishes washed and the laundry done.
Concurrency: you do not wait to finish one before starting another. You can start the dishes first for example, or start both at the same time. They can finish in any order, i.e. even if you started the dishes first, maybe the laundry gets done first.
Parallelism: you have more than one person in the house doing the work; as an example, you could work on the dishes, and the other person can do the laundry.
Asynchronous: You tell someone to do the laundry and you tell someone to do the dishes. They can actually be the same person (i.e. you tell them to do the laundry and immediately also tell them to do the dishes). They report back to you when they're done with each.
Synchronous: You tell someone to do the dishes. You wait for them. When they are done, you can do something else (you can tell them to do the laundry next, or you can do it yourself, or you can do something else entirely; point is you are blocked on the completion of the first task, you are in sync with them).
To summarize
concurrent when multiple things appear to happen at the same time (with the power of fast switching between concurrent tasks; they are concurrent because each task wants a piece of a resource, CPU, etc.)
parallel when multiple things truly happen at the same time (the number of executed threads is tightly related to the number of executing cores)
asynchronous is simply put non-blocking, when things we have to wait for don't leave us busy waiting (some sort of notification mechanism is required to continue from the point we left off)
CONCURRENCY VS PARALLELISM: concurrency at one point of time only one task can be done. example: single cpu processor parallelism at one point we can do multiple tasks. example: dual core or multi core processor
I'm giving real world scenario to explain 3 topics let's say you want to travel Ahmedabad to Mumbai but you don't know way, so you decided to take help of map application (Google Maps).
very normal but inefficient way is you can watch full path before starting your car and then you start driving and reach destination.
- Parallelly - You can drive and observe path constantly.
- Async - You have your friend in car with you and you gave him your mobile with map app opened and told him to watch map and guide you.
- Concurrent - You drive for few kilometers and park car a side and watch map, get directions and start driving again and so on.
Parallelism happens when a manager has several workers, and can give each of them a separate task. The workers do their jobs and provide the manager with the results. If the tasks cannot be fully separated, for example have some dependency to each other results or need the same resource to be dedicate without other inference, the parallelism degree is bound to such constraints and cannot be fully achieved.
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.
Concurrent + Parallel both mean running multiple tasks at the same time. I see some suggesting there is a distinction, but that varies depending on which reference you consult, and there is no real right or wrong answer.
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.
精彩评论