Concurrency vs Parallelism
Clear the confusion about parallelism and concurrency, and what tools Java provides to enable each concept.
Channel
----------------------------------
Complex concepts explained in short & simple manner. Topics include Java Concurrency, Spring Boot, Microservices, Distributed Systems etc. Feel free to ask any doubts in the comments. Also happy to take requests for new videos.
Subscribe or explore the channel - https://youtube.com/defogtech
Popular Videos
----------------------------------
What is an API Gateway - https://youtu.be/vHQqQBYJtLI
Executor Service - https://youtu.be/6Oo-9Can3H8
Introduction to CompletableFuture - https://youtu.be/ImtZgX1nmr8
Java Memory Model in 10 minutes - https://youtu.be/Z4hMFBvCDV4
Volatile vs Atomic - https://youtu.be/WH5UvQJizH0
What is Spring Webflux - https://youtu.be/M3jNn3HMeWg
Java Concurrency Interview question - https://youtu.be/_RSAS-gIjGo
Video Summary & Chapters
No chapters for this video generated yet.
Video Transcript
Let's start with parallelism
Let's take a look at this code
Here we have a main function and in the main function. We are starting two threads in
the first thread we are calling this function called process tax for user 1 and
In the second thread we are calling the process tax for user 2
Once we start these two threads, we are calling this function called heavy calculations
operations, which is essentially a task 3.
So we have 3 tasks, task 1 and task 2 are run on separate threads and task 3 will be
run by the main thread.
If we use Java 8, we can reduce this code to look something like this.
If we run this program on a quad core CPU, which means that there are four cores in our processor,
then we can run these three tasks in parallel. In OS, there is a component called scheduler,
which is responsible to schedule the threads onto the CPUs. So in this case, it is possible that
the main thread will run on core 1 and thread 1 and thread 2 will run on core 3 and core
2 respectively.
Since none of the tasks are dependent on each other, all three tasks will run on three separate
cores in parallel.
So parallelism is about doing a lot of things at once so that we can speed up our program.
We can very well write the same code which we saw and here instead of creating our own
threads we can also create a thread pool and we are creating a fixed thread pool of size
4 here and we are submitting two tasks.
So even in this case it is possible that on the CPU three cores are used to run these
three tasks simultaneously.
So in Jawa
To enable parallelism, we can either use raw threads like we did earlier or we can use
this concept of thread pool.
Even in thread pool, it could be an executor service, it could be the newer fork join pool
or it could be custom thread pools which are used by web servers.
But in all the cases, we'll have to ensure that our CPU has more than one cores so that
And we can run multiple threads parallelly to speed up our program.
So that was parallelism, relatively straightforward.
Now let's look at concurrency.
For concurrency, let's look at this code.
Here we have a main function and we have two threads again, thread 1 and thread 2.
and in thread 1 we are checking if the tickets available to book are more than 0.
If they are more than 0 then book the ticket and decrement available tickets count.
We are starting that thread and in thread 2 we are doing the exact same thing.
So the idea is we can book the ticket only if there are available tickets and once we
book the ticket we will reduce the available tickets by 1.
After starting two threads, we'll just put the main thread to sleep for few seconds.
So here essentially we have two tasks and both the tasks are accessing this shared variable.
This time let's assume we have only one core on our CPU.
Even in this case, our scheduler which is responsible for scheduling the threads onto
the CPU have the same job.
But it's trickier now because there are no multiple cores.
So, the scheduler will have to do some time sharing between the threads and to be fair
with the threads, it is possible that scheduler will schedule the thread 1 for a few milliseconds
and then kick thread 1 out and put thread 2 in for few milliseconds.
Once that few millisecond passes, it will again schedule thread 1 to execute and so
on and so forth.


Install Tubelator On Chrome