Concurrency

What does it mean? And how can it help my life?

Most of my learning happens subconsciously. Because of that, I rarely have a means of showing what I know apart from talking with people around me. This article is the beginning of an attempt to consciously document what I have been learning and what I know. It is also the first one in a series called "Concurrency in Java".

What is Concurrency?

I won't be giving a formal definition of concurrency. Instead, I'll give a situation that depicts concurrency and leave it to any interested reader to define it formally.

It's a bright new day and Daniel just woke up. He already has a list of activities he performs each morning. I've listed it out below:

  1. Pray

  2. Brush my teeth

  3. Boil water for my bath

  4. Take my bath

  5. Dress up for the day's work

  6. Prepare and eat breakfast

  7. Order a ride to work

This seems simple enough, yet Daniel still gets to work late almost every day. So Daniel decided he needs to remove some activities from his morning schedule, but he just couldn't seem to decide on which activity to remove, they all seemed to be important for him to have a great day. Can you help Daniel out?

Let's see what the bottleneck in Daniel's schedule is. First, he prays, then he brushes his teeth. After that, he boils water for his bath and then takes his bath. Do we notice something odd here already? Daniel has to wait for the water to boil before he can move on with his schedule. We've identified the first thing wasting Daniel's time. Let's continue. After taking his bath, Daniel dresses up, prepares and eats breakfast. He then orders a ride. Do we see it again? Daniel is again engaged from when he dresses up to when he eats breakfast. But then, he has to wait for his ride to arrive. That's another waste of time. Let's not talk about the time he'll spend in traffic.

Daniel's Morning Schedule

Daniel's morning schedule.

Knowing that Daniel's current schedule is not so efficient, what advice would you give to him? Are we thinking the same thing? Why would Daniel not reorder his morning schedule? There are two points where he could reorder his schedule.

  1. He could make boiling water the first or second activity on his list. That way, while the water is boiling, he prays and/or brushes his teeth.

  2. He could also order his ride before preparing and eating breakfast. He just prepares and eats his breakfast while waiting for the ride.

The suggested morning schedule for Daniel would look like this:

  1. Boil water for my bath

  2. Pray

  3. Brush my teeth

  4. Take my bath

  5. Dress up for the day's work

  6. Order a ride to work

  7. Prepare and eat breakfast

Daniel's suggested morning schedule. Notice the time he saves.

This concept that we have used in making Daniel's schedule more efficient is what we call concurrency. As you can already imagine, concurrency plays out in our daily lives. But humans are not the only ones that can benefit from concurrency, our computers can also be made more efficient using concurrency. Some computer processes - like input and output - do not make full use of the CPU, but prevent other processes from running. We will call these processes blocking tasks. So we ask ourselves, why not run other tasks while we wait for these blocking tasks? That's exactly what we do and what we mean by concurrency.

Concurrency Is Not Parallelism

What does this even mean? To answer that, let's understand what parallelism means. Just like concurrency, it's not a difficult concept. Now, suppose Daniel had many arms. He could actually order a ride while dressing up and preparing breakfast. (Don't ask me how exactly.) And no, this is different from making use of his waiting time for the ride ordered to dress up and prepare breakfast. In this case, he could actually do three things at the same time. This is what parallelism is about, executing a number of tasks in parallel. Can you see how this is different from concurrency?

Daniel's supposed schedule if he had many arms.

Concurrency (and parallelism) is not a magical solution to your problem. It is notoriously hard to get right and needs to be implemented carefully to give any real benefit. Java has built-in support for concurrency (and parallelism). The next article in this series will look at what that support looks like.