Concurrency vs Parallelism – Understanding Differences

In the previous article, we went through the concept of Concurrency. But the question on the concurrency vs parallelism always comes around.

Because of this confusion and sometimes misunderstood concept, we get far away from understanding the subject of multithreading in deep.

But don’t worry. By the end of this article you will:

  1. Understand the difference between concurrency and parallelism
  2. Their relationship with single core and multi-core systems
  3. Be able to recognize a concurrent and/or parallel system

So, stay tuned!!

This article is part of our series on multithreading in Java.

What is Concurrency?

As already discussed, Concurrency is the ability to handle multiple things at same or overlapping time.

What is Parallelism?

Parallelism is the ability to execute multiple things exactly at the same time.

Easier said than understood. No worries. Let’s understand this in detail.

Concurrency vs Parallelism

So, the concurrent application we discussed in the concurrency article we had:

  1. The Main Thread
  2. And Query Handler Thread.

NOW, we will understand concurrency vs parallelism with regards to this application with the help of different scenarios.

Scenario #1 – Concurrency vs Parallelism in single core system

Let’s put our concurrent application on a system which has single CPU core. What do you think will happen?

Both the threads will get the chance to execute their tasks. But, do you think that they can do that exactly at the same time in a single core?

It is not even possible. They get the CPU cycles because of operating system’s thread scheduler which enables time slicing.Overlapped timings but not in parallel.

Hence, in this scenario, our application is concurrent but not executable in parallel.

Role of CPU Hyper-threading

Do you know what is a hyper-threaded CPU core? It is threading on the hardware level!!

Now-a-days, every CPU core is hyper-threaded (typically by 2) meaning they can execute 2 OS threads at the same time.

So, a quad core CPU can execute 8 OS threads. Similarly, Hexa Core can execute 12 threads.

Hence, in real world, our current scenario is invalid because at any point of time, single core will work like multi-core(2 threads). So, no single core scenario.

So, this scenario makes sense for the sake of Java interviews or better understanding of every aspect. Hold tight and now let’s move towards our next scenario.

Scenario #2 – Concurrency vs Parallelism in multi-core system

NOW, let’s put the same concurrent application in a multi-core system. What do you think will happen? It is obvious now, isn’t it?

Both the threads will be executed of-course. But as the operating system has access to multiple CPU cores, it can execute these threads in parallel.

So, in multi-core system, our concurrent application follows parallelism too.

Scenario #3 – Non-Concurrent application in single or multiple cores system

If the application is built in such a way that it can not handle two or more things at the same time, then it is not concurrent. Take the example of our non-concurrent application from our earlier article.

It doesn’t matter if the system is single core or multi-core: Application is not concurrent. That’s it. Full Stop.

And if the application is not concurrent, it means that it does even not have multiple units which can be executed in parallel.

It doesn’t matter how many CPU cores a system have, if the application do not have multiple things to run, it won’t be run.

Hence, a system which is not concurrent can never be executed in parallel.

Key Points on Concurrency vs Parallelism

  1. If an application is not concurrent, it can not run in parallel. Concurrency enables parallelism.
  2. A concurrent application on single CPU core can never run in parallel.
  3. In a hyper-threaded single CPU core, a concurrent application can run in parallel.
  4. A concurrent application on multi-core CPU will almost always run in parallel.

What to read next?

Let’s now understand the difference between concurrency and multithreading. With this, we will also see what should be the target of a Java developer while building a Java application.

Go to: Concurrency vs Multithreading.

Leave a Comment