How is concurrency different to parallelism?

FixIT21 report abuse

Hi. I am a student in software engineering. Recently, I learned that there are concurrency and parallelism concepts in programming. But for me, they sound pretty similar. I can't see the difference between them. So, my question is what the difference between these concepts, if there is any difference.

Answers

jeluw report abuse

Concurrency is a wider concept than parallelism. In other words, parallelism is the form of concurrency, but there are other forms also. Concurrency is the situation when the execution of several tasks overlaps. Parallelism is the process where several tasks execute simultaneously.

FixIT21 report abuse

Well, I understand that concurrency includes parallelism. But I don't understand from the definitions why concurrency cannot be called parallelism. The overlapping of the execution time of several tasks is also can be described as the simultaneous execution, isn't it?

OlesB report abuse

Maybe you should look at the following situation to understand this better. This is the example is concurrency which is not parallelism. Suppose that the task1 started at second 0, has been executing until second 2, then was waiting for the external input until second 6, and then finally run for 4 seconds and was completely finished at second 10. But there also is the task2. This task was started at the second 2 (when the task1 started waiting), has been executing until second 4, then was waiting for the external input until second 8, then was waiting until the task1 will be finished at the second 10, then ran for 4 seconds and was completely finished at second 14. So, as you can see, each task takes 10 seconds to fully complete their work (2 seconds of initial execution + 4 seconds of waiting for the external input + 4 seconds of the input processing and producing the result). If we run them sequentially, we would spend 20 seconds in total. But as we use concurrency, we save time by running the task2 when the task1 was sleeping (waiting). And in total, we spent just 14 seconds for the completion of 2 tasks instead of 20 seconds. The tasks were not executing simultaneously, but their execution time overlaps.

jeluw report abuse

Yes, this is a good example, OlesB. I want to add that in multiprocessing, task1 and task2 would be executed simultaneously in different CPUs or CPU cores. But the total execution time wouldn't be just 10 seconds, because it takes time to set up separate processes on separate CPUs. Read more about IO-bound tasks and CPU-bound tasks, I think you will find a lot of interesting things there.

FixIT21 report abuse

Thank you for your examples and advice! That was very useful for me.

Add Answer

Need support?

Just drop us an email to ... Show more