"What is the difference between parallelism and concurrency?"

Regarding this question, many students who have worked for more than 5 years cannot answer it.

In other words, I have a certain understanding, but I don't know how to express it.

Hi, my name is Mic, a Java programmer who has been working for 14 years.

What is the interviewer looking for in this question?

Problem Analysis

Parallelism and concurrency actually described the concepts in Java concurrent programming.

What they emphasize is the ability of the CPU to handle the task.

In simple terms, concurrency is the number of tasks that the CPU can process at the same time, and for applications, there will be no lag.

Parallelism means that multiple tasks are allowed to execute at the same time. In a multi-core CPU architecture, the number of tasks to be executed at the same time is determined by the number of cores. For example, in a CPU with 4 cores and 4 threads, only 4 threads can be executed at the same time.

These two concepts look similar, but in fact, the latitude of description is different. Concurrency describes the perspective of program processing power.

Parallelism describes the perspective of how the CPU handles tasks, one at the macro level and the other at the micro level.

The two of them complement each other. The ability of the CPU to execute tasks in parallel can also improve the concurrent processing performance of the program.

So the performance of multi-core CPU is better than that of single-core CPU.

Of course, if it is a single-core CPU, the concurrency capability can also be improved by switching time slices.

Joe Armstrong, the father of Erlang, explained the difference between parallelism and concurrency with a picture.

Concurrency is when two queues use one coffee machine alternately, and parallelism is when two queues use two coffee machines at the same time.

preview

Therefore, in my opinion, this interview question can be a good test of the applicant's understanding of Java concurrent programming.

There are many articles on the Internet that try to explain this concept, but these explanations make the problem more and more complicated.

I think that only a deep understanding of the underlying principles of threads can answer this question well.

Expert:

Parallelism and concurrency are concepts in Java concurrent programming.

Parallelism refers to the ability to execute multiple threads at the same time at the same time under the multi-core CPU architecture.

In a single-core CPU architecture, only one thread can run at a time.

In a CPU architecture with 4 cores and 4 threads, 4 threads can run at the same time, and these 4 threads are executed in parallel.

Concurrency refers to the number of tasks that the CPU can process at the same time, which can also be understood as the concurrency capability of the CPU.

In the single-core CPU architecture, the operating system improves the concurrency capability of the CPU through the CPU time slice mechanism

In the multi-core CPU architecture, the concurrency capability of the CPU is improved based on the parallel execution capability of tasks and the capability of CPU time slice switching.

So, in general, concurrency is a macro concept, which refers to the amount of pressure the CPU can carry, and parallelism is a micro concept, which describes the ability of the CPU to perform multiple tasks at the same time.

Summarize

Well, today's sharing ends here

If you like my work, remember to like, favorite, and follow

Need a collection of Java interview questions to scan below

Copyright notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated. Please indicate the source for Mic带你学架构 !
If this article is helpful to you, please help to follow and like, your persistence is the driving force for my continuous creation. Welcome to follow the WeChat public account of the same name to get more technical dry goods!

跟着Mic学架构
810 声望1.1k 粉丝

《Spring Cloud Alibaba 微服务原理与实战》、《Java并发编程深度理解及实战》作者。 咕泡教育联合创始人,12年开发架构经验,对分布式微服务、高并发领域有非常丰富的实战经验。