Introduction to the

In modern programs, we often use two keywords: concurrency and parallelism. Although the English words of the two are very different, they are almost the same when translated into Chinese. Although Chinese trumps English with its beautiful grammar and neat writing, the resulting complexity and multi-meaning of translation can often be a little annoying to technical workers.

That’s all right. Today’s article is to decipher the connections and differences between concurrency and parallelism.

Note that the concepts of concurrency and parallelism in this article refer to the same application.

Concurrency and parallelism

In fact, in addition to Concurrent concurrency and Parallel parallelism, there are two states: Parallel Execution and Parallel Concurrent Execution. Let’s talk about the differences between them.

Concurrent concurrency

You know that Java has a very useful concurrent package called java.util.concurrent, which has a number of very useful classes for handling resource contention between multiple threads. Given the role of concurrent dispatching, you should be able to guess that the biggest difference between concurrency and parallelism is whether or not there is preemption.

Let’s take an example of the recent outbreak of COVID-19 vaccine. Before there were no confirmed local cases, no one was in a hurry to get the vaccine, so there was no need to grab more resources, so there was no concurrency problem.

But when a confirmed case appeared in one place, everyone panicked and rushed to get vaccinated, straining resources and creating concurrency problems.

To better describe the concurrency problem, suppose we have 10 people in two lines trying to get vaccinated, and there is only one window to get vaccinated. One likely strategy, then, is to alternate vaccinations between the two teams. Let’s draw a graph to show:

<img src=”https://img-blog.csdnimg.cn/20210527195139795.png” style=”zoom:50%;” />

The image above shows the concurrency situation. A window can only handle one task at a time, so two teams are competing for the window resource.

A variety of locking issues arise during resource contention, which requires extra care.

Parallel Execution

Parallel execution means that two tasks are performed at the same time without interfering with each other. In other words, there is no competition between tasks for resources, so there is no locking problem. In the case of vaccines, parallel execution means that you now have two Windows, and each queue can get a window, so there’s no race.

<img src=”https://img-blog.csdnimg.cn/20210527200221921.png” style=”zoom:50%;” />

Parallel execution is the most ideal situation in program execution, where resources are sufficient and only the specific business logic needs to be considered, without considering the interaction and resource occupancy between them.

Parallel Concurrent Execution

Parallel concurrent execution means that there is concurrency in the parallel process. Take vaccination, for example, now there are two stadiums, each stadium has only one window for vaccination, and for both stadiums they are parallel. But for each window in each stadium, it executes concurrently.

<img src=”https://img-blog.csdnimg.cn/20210527200758968.png” style=”zoom:50%;” />

The parallel concurrent execution state should be the basic state in a generic application. Threads performing different tasks execute in parallel, and their resources are isolated, so they do not affect each other. However, multiple threads executing the same task are concurrent, and they will preempt resources among them, so concurrency control is needed.

parallelparallelism

There is no significant difference between parallelism and parallelism, although the former is a professional computer name for parallelism, and the latter can be used anywhere to refer to parallelism.

So what does parallelism mean in computers?

It refers to the degree to which a task can be parallel. For example, the task of vaccinating 5 people can be divided into 5 groups, each group can strive for its own resources to carry out the task, which can be concurrent or parallel, which is parallelism. We use the following diagram to show:

<img src=”https://img-blog.csdnimg.cn/20210527202048311.png” style=”zoom:50%;” />

conclusion

So, do you see the difference?

This article has been included in http://www.flydean.com/05-concurrency-parallelism/

The most popular interpretation, the most profound dry goods, the most concise tutorial, many you do not know the tips to wait for you to discover!

Welcome to pay attention to my public number: “procedures those things”, understand technology, more understand you!