324

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?

Laurel
  • 5,965
  • 14
  • 31
  • 57
GurdeepS
  • 65,107
  • 109
  • 251
  • 387
  • 18
    The term "asynchronous" can mean a lot of different things. Those terms are related, but they do not describe disjoint sets of things. The meanings overlap and vary by situation. – Pointy Jan 30 '11 at 18:26
  • 2
    So first concurrency is running two or more processes at the same time. With that out of the way, being concurrent is not being parallel. Parallel processes require two or more cores whereas concurrent processes can time share a single core. – Rick O'Shea Jul 28 '18 at 01:33
  • This is a near-duplicate of https://stackoverflow.com/questions/1050222/what-is-the-difference-between-concurrency-and-parallelism, which also has some good answers. The difference is that this question asks about asynchronous methods, while the other doesn't. – Maxpm Jan 10 '21 at 01:22

17 Answers17

236

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.

Ijas Ameenudeen
  • 9,069
  • 3
  • 41
  • 54
Lazarus
  • 41,906
  • 4
  • 43
  • 54
  • 75
    I'd argue that you have parallelism and concurrency mixed up in your first paragraph. Concurrency refers managing multiple threads of execution, where parallelism is more specifically, multiple threads of execution executing simultaneously. Concurrency is the broader term which can encompass parallelism. – Mark H Jan 30 '11 at 19:37
  • 11
    While the two words are very similar and could be confused (and often are), they do have different definitions: Concurrent = Existing, happening, or done at the same time. Parallel = of or pertaining to the apparent or actual performance of more than one operation at a time, by the same or different devices. As you can see parallel does not necessarily mean concurrent but could be just appearing to be concurrent. At the end of the day, the words are often used interchangeably and with *n* dev is a room you'll probably get *n+1* definitions ;) – Lazarus Jan 31 '11 at 15:12
  • 3
    I also think you have concurrency and parallelism mixed up. Parallel computations happen simultaneously. (I don't know about you, but I've never seen multi-core CPUs as advertising their capability of "concurrent" execution -- they always talk about parallel execution.) Concurrent ones simply overlap. I'm not sure how asynchrony is different from concurrency though. – user541686 Nov 16 '12 at 10:24
  • 6
    @Mehrdad If you are going to base your definitions of dictionary words on their use in marketing materials then I think you are likely to find yourself at somewhat of a disadvantage. – Lazarus Nov 27 '12 at 08:27
  • 21
    Wrong. In the context of programming, concurrency is the ability of your code to be "composed" into bits of logic that *could* be run at the same time. Parallelism (when combined with concurrency) is taking said code and running it on a 100-core machine. – Frank Radocaj Feb 23 '14 at 01:11
  • The popular async library **rxjava is single-threaded** by default as well but as the author points out, you can can make it fork separate threads, e.g. using `.subscribeOn(Schedulers.io())` resp. `.oberserveOn(Schedulers.io())` and achieve actual parallelism by calling `.parallel()`. – Alex Feb 27 '16 at 22:54
  • 6
    @FrankRadocaj has it right. Concurrent means that the program can be split up into units (units essentially being threads) that can be ran in any order and have a determinate outcome. Parallel means these units/threads are being ran literally at the same time on multiple processors. –  Apr 26 '17 at 09:23
  • @MarkH That doesn't sound quite right. Concurrency is not a subset of parallelism, and parallelism is not a subset of concurrency. A multi-threaded application running on a single-core machine exhibits concurrency without parallelism. SIMD exhibits parallelism without concurrency (as there is just one execution stream). – Max Barraclough Jan 10 '21 at 23:38
  • For anyone interested in topic, please consider reading http://wiki.c2.com/?SharedStateConcurrency (thoughtfully). After that, you could be clearly seeing that, of course, concurrency is not parallelism. Next is the answer. – Anthony Nov 20 '21 at 22:54
  • It is concurrency who allows for parallelism – Anthony Nov 20 '21 at 23:04
  • It is operating system who allows you to read this in your browser, listen to music and take notes in editor __simultaneously__. Whis s concurrent OS. But this is hardly ever parallel f you have only one-core, one thread (Intel calls it HyperThreading) CPU. This is rare nowadays. – Anthony Nov 20 '21 at 23:09
164

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.

enter image description here


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.

enter image description here

enter image description here

Dimos
  • 8,330
  • 1
  • 38
  • 37
  • 22
    Simple, effective illustration. – contactmatt Mar 12 '18 at 15:33
  • 1
    is concurrency the same as asynchrony? – nos Feb 18 '19 at 03:52
  • 2
    These 2 concepts are very close, indeed, but not the same. In practice, asynchrony is more related with the interaction between actions (say A & B), where one (B) is triggered by the other (A) and whether the second one will wait on the first one to complete. Concurrency is a more general terms for actions that can also be unrelated to each other and whether they are executed in sequence or their executions are interleaved. – Dimos Feb 18 '19 at 19:22
  • 3
    So asynchrony is mostly about blocking and non blocking – Daniel May 22 '20 at 17:46
  • 7
    Your definition on concurrency is wrong. Concurrency neither means non-parallel nor means parallel, it just means being able to break a program into multiple parts and re-ordering them arbitrarily, so each of which can be run by a separate thread, but that doesn't say anything about running in parallel or not. Concurrency is about dealing with "lots of things at once", Parallelism is about dealing with "doing lots of things at once". "__Concurrency _allows_ Parallelism"__: [Wikipedia](https://en.wikipedia.org/wiki/Concurrency_(computer_science)) – aderchox Apr 15 '21 at 07:20
  • For me this is the best answer – Aji Saputra Raka Siwi Nov 15 '21 at 05:09
  • 1
    @aderchox I don't see anything wrong in Dimos's definitions. – RookieScientist Jun 19 '22 at 23:22
59

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.

rahulaga-msft
  • 3,964
  • 6
  • 26
  • 44
  • 3
    These are really good analogies! Thanks. Would it be fair to say concurrency can be defined with your multithreading definition? So concurrency = multithreading on a single-core which looks like it's happening at the same time but it's really switching back and forth really quickly? – wongz Jun 07 '20 at 14:46
26

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.

Dhirendra Gautam
  • 737
  • 8
  • 16
24

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.

10

"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).

Pedro Boechat
  • 2,446
  • 1
  • 20
  • 25
  • 4
    medium is not a source, it is an article by someone else talking about his (mis)understanding of a subject, it doesn't make him/her an authority. – Moha the almighty camel Feb 29 '20 at 22:12
  • 6
    It's a citation, therefore the source. Most answers here are not written by authorities in any field and the explanation the author gave is good enough. – Pedro Boechat Feb 29 '20 at 23:03
  • At stackoverflow you at least have a voting system, and it's a community of professionals. Anyone can write anything on medium. It's not a fair comparison between the two. – Moha the almighty camel Mar 01 '20 at 07:06
  • 4
    Anyone can write anything here too, I don't know why you're picking on Medium. Anyway, I'm a professional programmer and I endorse this understanding. And I find it elegant because its relatively short. – Pedro Boechat Mar 01 '20 at 08:50
  • The problem is not that Medium is a bad source, the problem is that an answer that merely links to an external page is not an answer. You could greatly improve this answer if you elaborated on the quoted portion or summarized the contents of the article rather than just linking to it. – David Schwartz Jun 16 '20 at 19:47
  • 1
    I think it was my bad to assume it was self-explanatory. – Pedro Boechat Jun 17 '20 at 10:00
10

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.

Testaccount
  • 2,755
  • 3
  • 22
  • 27
8

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

Community
  • 1
  • 1
LONGHORN007
  • 526
  • 9
  • 24
6

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 ;)

jeancallisti
  • 1,046
  • 1
  • 11
  • 21
6

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.

Evans AB
  • 61
  • 1
  • 3
5

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).

Alan
  • 2,897
  • 4
  • 23
  • 27
3

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.

  1. Parallelly - You can drive and observe path constantly.
  2. 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.
  3. Concurrent - You drive for few kilometers and park car a side and watch map, get directions and start driving again and so on.
3

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)

orustammanapov
  • 1,792
  • 5
  • 25
  • 44
  • In Python `await asyncio.sleep` is considered async and that is blocking. So, async doesn't mean non-blocking. I don't think asynchronous has a real meaning. Also, the distinction drawn here between concurrent and parallel seems arbitrary and fabricated. – clay Jun 27 '22 at 19:45
  • Hey @clay, `async/await` is usually either a language or (like in this case) a [library](https://docs.python.org/3/library/asyncio.html) construct. What makes you think, that it has to be non-blocking all the time? Just to clarify `asynchronous` is not equal `async/await` (see "await" there :)). `async/await` is not inherently `fire-and-forget` either, what seems to be your understanding here. Whenever you have to wait for result it becomes blocking. In case of `fire-and-forget` though, you don't have to wait for anything and hence non-blocking until the end. – orustammanapov Jun 28 '22 at 06:14
2

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

rva
  • 21
  • 1
1

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.

shvahabi
  • 1,218
  • 10
  • 7
0

I am trying to answer in context of C# programming.

Concurrency: Concurrency is a programming concept where the programmer can divide the program into independent parts (threads) and can execute them in order-independent manner or in partial order, without affecting the outcome. In practical, we achieve this using multi-threading. On single core processor, tasks (threads) execute in interleaving method. That means, the same core switches from one thread to the other. So at-a-time, only one thread executes. On multi-core processor, tasks (threads) execute as "concurrent + parallel method", as there is a possibility of two or more tasks (threads) execute simultaneously on different cores.

Parallelism Parallelism is the ability of a program to execute multiple tasks simultaneously, typically by utilizing multiple CPUs or processing cores. As mentioned above, it can be combined with concurrency.

Asynchronous Asynchrony is a programming technique where you perform non-block I/O operations. Means, a task (thread) is held until you will get a response from a resource (database or network request); but the thread need not wait for completion of the task. It can execute some other task (thread).

Note: Here, the term "task" should not be confused with "tasks" in TPL (Task Parallel Library).

Harsha
  • 101
  • 1
  • 5
-3

Concurrent + Parallel: These have the same meaning. Concurrent is defined in the Merriam Webster dictionary with the primary definition: "operating or occurring at the same time". Parallel means the same thing. Often, threads of execution are displayed as lines on a diagram, and when those lines are parallel, the threads are executing at the same time. Several sources suggest some distinction between the two terms "concurrent" and "parallel", but that is highly dependent on which source you look at, and there isn't a widely agreed upon distinction.

Asynchronous: Literally, this means not-synchronous; it means that when a function or procedure returns, the task isn't necessarily completed, and often the procedure will return some kind of Future/Promise or task handle, that may or may not be completed.

clay
  • 18,138
  • 28
  • 107
  • 192