This article, the third in a series that illustrates concurrent Programming in Java, is accessed as follows:

  • Process threads, mutex, and semaphores – Hit me if you don’t understand
  • More than 80% of the Java thread state diagrams are drawn incorrectly

1. CPU concurrency and parallelism

As a child who does not love learning, I often do such things when I go to school: make up the physics homework in the math class, the math class understood, and the physics homework was also handed in. To be immodest, I have such ability, but it is a pity that it is not in the right place.

Many students said, “Do you have more than one brain?” Of course not. It’s the ability to switch. When you’re doing more than one thing at a time and can switch quickly. You have the ability to be concurrent with others, but your brain is still a brain.

Our computers run many, many processes and threads most of the time, so it is normal for the CPU to execute concurrently and switch between allocating CPU time slices. It’s just that the CPU is executing so fast that most of the time you won’t even notice the “thread switch.” So it looks like it’s playing music, running a browser, and running other software at the same time.

Parallelism now that we know the concept of concurrency in a computer, let’s illustrate the concept of parallelism.

  • You order your food in a restaurant, and you’re reading while you’re waiting. It looks like you’re doing two things, waiting for dinner and reading. There’s only one thing you actually have to do, and that’s read books; The other thing is actually cooking, and cooking is done by the chef in the restaurant.

With the popularity of multi-core CPUS in computers, computers are also capable of parallel computing to a certain extent. So there must be no parallelism in a single core CPU? This is not entirely true. If a single core CPU has hyper-threading technology, then a single core can run two logical threads in parallel.

Concurrency and parallelism from the perspective of threads

The above explanation of concurrency versus parallelism is more from a CPU run point of view, but as a programmer, it is more of a developer’s concern to distinguish between concurrency and parallelism.

Talking about parallelism from the perspective of threads usually means that there is no data sharing or communication between multi-threads, and the parallel ability of CPU can be used to run multi-threads.

When we talk about concurrency from the perspective of threads, we usually mean that multiple threads share resources (memory, code segments, etc.) and coordinate the use of shared resources, thus giving up or gaining access to CPU execution time slices.

To sum up a sentence, I hope it will be helpful for you to understand concurrency and parallelism. Concurrency is the seeming parallelism, but the actual switching of resources. The purpose of concurrency is to maximize and optimize the utilization of resources.

Welcome to my blog, where there are many fine collections

  • This article is reprinted with a credit (must be accompanied by a link, not only the text) : Antetokounmpo blog.

Feel helpful to you, help me like, share! Your support is my inexhaustible creative power! . In addition, the author recently a period of time output as follows boutique content, looking forward to your attention.

  • Spring Boot2.0 by Hand
  • Spring Security- JWT-OAUTH2
  • RBAC Authority Management System for Actual Combat Front-end and Back-end Separation
  • “Actual SpringCloud Micro-service from Bronze to King”
  • VUE Series