One day I was doing a phone interview from my home after work and asked candidates, “Can you briefly explain what you understand about concurrency and parallelism and how they relate to each other?” However, the interviewees did not respond well, so I wrote in the interview evaluation: “I am not clear about the concepts of concurrency and parallelism”. At this time, the girlfriend saw this sentence.

Concurrency and parallelism were originally concepts in operating systems, representing the way a CPU performs multiple tasks. It is easy to confuse the two concepts.

“Simultaneous”

With the Windows operating system we use today, you can do many things at once. For example, we can chat on QQ while watching movies. While listening to music, while playing games.

However, this so-called “simultaneous”, in the operating system may not be the true sense of the “simultaneous”.

In fact, on a single-CPU computer, you can only do one thing at a time on the CPU. To look like “multitasking,” operating systems like Windows divide CPU time into shorter, nearly equal time segments, known as “time slices,” which are managed by the operating system and allocated in turn to various applications.

This gives the user the impression that he is listening to music and playing the game at the same time. In fact, in the operating system, the CPU switches back and forth between the game and the music player.

There is a rule for the use of time slices in the operating system: if a job is not completed before the end of the time slice, the job is suspended, the CPU is abandoned, and the next cycle is resumed. The CPU is then allocated to another job.

Let’s focus on the EXECUTION of the CPU, which, when scaled up, looks like a phone booth. Multiple users do not use the phone in the booth at the same time, but take turns.

Due to the speed of computer processing, as long as the time slices are properly spaced, a user’s job will “pause” between the time slice allocated to it and the time slice of the next CPU, but the user will not notice it.

So what looks like “multitasking” on a single-CPU computer is actually doing it concurrently, using CPU time-slice technology.

concurrent

Concurrent, “Concurrent,” in an operating system, refers to a period of time in which several programs are running between start and completion and are all running on the same processor.

Think of the operating system time slice scheduling mentioned earlier. Playing games and listening to music are completed from start to finish on the same computer in the same period of time. So it can be said that listening to music and playing games are concurrent.

parallel

Parallel, when the system has more than one CPU, when one CPU executes a process, the other CPU can execute another process, the two processes do not occupy CPU resources, can be carried out at the same time, this method is called Parallel.

One important point is that the system must have multiple cpus for parallelism to occur. True “simultaneous” only occurs when there are multiple cpus.

Concurrency and parallelism

The two of us are having lunch. You eat rice, you eat vegetables, you eat beef throughout the meal. Eating rice, vegetables, and beef are all done concurrently. To you, the whole process seems to happen simultaneously. But you’re switching back and forth between eating different things.

It’s just the two of us having lunch. In the course of the meal, you had rice, vegetables, beef. I also ate rice, vegetables and beef. The two of us eat in parallel. Two people can eat beef together at the same time, or one can eat beef and one can eat vegetables. They are mutually exclusive.

The difference between concurrency and parallelism

Concurrency is when multiple things happen at the same time at the same time. Parallelism means multiple things happening at the same time.

Concurrent tasks preempt each other’s resources. Parallel tasks do not grab resources from each other.

Parallelism occurs only in the case of multiple cpus. Otherwise, everything that seems to happen at the same time is actually executed concurrently.

As shown in the picture above, when there is only one coffee machine, one coffee machine is actually being used concurrently. When there are multiple coffee machines, they are used in parallel.