Introduction

In modern programs, we often use two keywords: concurrent concurrency and parallel parallelism . Although the English words of the two are very different, they are almost the same after they are translated into Chinese. Although Chinese surpasses English with its beautiful grammar and neat writing, the complexity and the ambiguity of translation often make technical workers a little annoyed.

It doesn't matter, today this article will decipher the connection and difference between concurrency and parallelism for everyone.

Note that the concepts of concurrency and parallelism mentioned 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. Next, let's explain their differences separately.

Concurrency

Everyone knows that there is a very useful concurrency package in java called java.util.concurrent, which contains many very useful classes to deal with resource competition between multiple threads. According to the role of concurrent packages, everyone should be able to guess that the biggest difference between concurrency and parallelism is whether there is resource preemption.

Let's take an example of the recent outbreak of the new crown vaccine. Before there were no confirmed cases in the local area, everyone did not panic about the vaccine. The resources were relatively large and there was no need to grab the vaccine, so there was no concurrency problem.

But when a confirmed case appeared in one place, everyone panicked and rushed to get the vaccine. This caused a shortage of resources and caused concurrency problems.

In order to better describe this concurrency problem, suppose we have 10 people lined up in two teams to get the vaccine, and as a result, there is only one window for the vaccine. Then a very possible strategy is to alternate the window to vaccinate people from the two teams. Let's make a picture to show:

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

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

In the process of resource contention, various lock problems will occur, which requires special care.

Parallel Execution

Parallel execution means that two tasks that do not interfere with each other are performed at the same time. That is to say, there is no resource competition between tasks, so there will be no lock problems. If it is used in the issue of vaccines, parallel execution means that there are now two windows, and each queue can be assigned to one window, and there will be no competition.

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

Parallel execution is the most ideal situation in program execution. In this case, resources are sufficient, and only the specific business logic needs to be considered, and there is no need to consider the interaction between them and the resource occupation relationship.

Parallel Concurrent Execution

Parallel execution means that there is concurrency in the parallel process. Take the example of vaccination. There are two gymnasiums. Each gymnasium has only one window for vaccination. For the two gymnasiums, they are parallel. But for each window in each stadium, it is executed 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 general application. Threads that perform different tasks are executed in parallel, and their resources are isolated, so they do not affect each other. However, multiple threads performing the same task are concurrent, and resources will be preempted among them, so concurrency control is required.

Parallel parallelism

Parallelism and Parallel do not seem to have much difference in translation. The first one is a professional computer name to indicate parallelism, and the latter one can be used anywhere to indicate parallelism.

So what does parallelism mean in computers?

In fact, it refers to the degree of parallelism of a task. For example, for the task of vaccinating 5 people, 5 people can be divided into 5 groups, and each group can fight for its own resources to execute, which can be concurrent or parallel. This is the meaning of parallelism. We use the following diagram to represent:

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

to sum up

Having said so much, do you understand the difference between them?

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, and many tips you don't know are waiting for you to discover!

Welcome to pay attention to my official account: "Program those things", know technology, know you better!


flydean
890 声望433 粉丝

欢迎访问我的个人网站:www.flydean.com