In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article focuses on "what is the difference between concurrency and parallelism in Go language". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what's the difference between concurrency and parallelism in Go?"
Now we all talk about designing programs that can be parallelized and highly concurrency, and we often subconsciously feel that we have a clear distinction between Parallelism and Concurrency, but if we want to clearly tell the difference between the two, we feel that we cannot give a very clear description.
So what is concurrency? What is parallelism? The concept of parallelism is relatively simple, parallelism is always related to executions, and a lot of things are executed in parallel, while concurrency organizes your program in some ways, so that it can be divided into multiple modules to execute independently. Parallelism must be multi-core, and a processor cannot be parallel; but concurrency is not necessarily related to the processor. On a processor, our programs can also be concurrent.
To take a simple example, Hua Luogeng must have steps such as boiling water, washing cups, taking tea, and so on. Now we want to finish this as soon as possible, that is, "there are a lot of things to deal with". There are many ways to achieve concurrency, such as asking multiple people to do it at the same time, which is parallel. Parallelism is one way to achieve concurrency, but it is not the only way. We can also achieve concurrency by ourselves, such as boiling water first, and then washing cups without waiting for the water to boil, so concurrency can also be achieved by adjusting the way the program runs.
If you think the above explanation is still too abstract, here is a short story based on a speech by Rob Pike, one of the founders of the Go language.
There is a need at the beginning of the story: a group of gophers are going to push a bunch of abandoned instructions to the stove and burn them.
At first there was only one gopher, using a cart, loading the book into the car, transporting it to the fire, and unloading the book to the stove. It is bound to take a long time to complete the task.
It would be useless to add another gopher at this time, because one gopher is working and the other gopher can only wait. Of course, some people say that two gophers take turns using a cart, so that the gophers can get a rest, so that they can work faster and more efficient. )
Find another cart, and the two hamsters use their respective carts to load the books on the cart, transport them to the fire, and unload the books to the stove. This will improve the efficiency of transportation, but they will queue up when loading and unloading books, reducing efficiency.
This is faster than before, but there is still a bottleneck. Because there is only one pile of books and only one stove, we also have to coordinate the actions of the two gophers through messages. All right, let's divide the books into two piles and add a stove.
This is almost twice as efficient as before. Now this model is concurrent, because two hamsters can do one thing independently, which improves the transportation efficiency, and does not queue up when loading and unloading books, which improves the efficiency of loading and unloading. But the model is not necessarily parallel, for example, there may be only one gopher working at a time.
This is the first concurrency model, and we can design more concurrency models and continue to read comics.
This time, I found three gophers, one responsible for loading the books into the car, one for transportation, and the other for unloading the books to the stove and burning them. Each hamster does a separate task, and of course the three hamsters need to use some means such as message communication to coordinate.
The two gophers that load and burn books are easy, but the one in charge of transportation is very tired, and there is a bottleneck in the system. Then let's find another gopher and take it back to the empty trolley.
We added a concurrent step (the fourth gopher) to an existing design (the design of the three gophers) to enhance the performance of the system. In this way, two hamsters to engage in transportation, if well coordinated, the theoretical work efficiency will be four times that of a hamster.
There are 4 concurrent steps in total:
Load the books into the car
Carry the cart to the fire
Unload the book into the stove
Bring it back to the empty cart.
You can add another grouping to parallelize the concurrency model.
Let's take a look at another concurrency model. The hamster in charge of transportation complains that the transportation distance is too long, so we will add a transit station.
Then add another grouping to parallelize the concurrent model, and the two groupings are executed in parallel.
The above concurrency model can be further improved. At the same time of increasing the transfer station, two more gophers are added, one is responsible for unloading the books from the book pile to the transit station, and the other is responsible for loading the books from the transfer station to the cart, and then transporting the back gopher to the fire.
Then add another grouping to parallelize the concurrency model.
At this point, I believe you have a deeper understanding of "what is the difference between concurrency and parallelism in Go". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.