Golang - Concurrency, Parallelism, Sequence

王林
Release: 2024-02-09 22:00:10
forward
1028 people have browsed it

Golang - 并发、并行、顺序

php Xiaobian Xigua introduces you to the three important concepts of concurrency, parallelism and sequence in Golang. In concurrent programming, Golang provides a wealth of tools and syntax to implement concurrent operations, allowing the program to handle multiple tasks at the same time. Parallel programming is to execute multiple tasks at the same time and improve program execution efficiency by using multi-core processors. Sequential programming is executed in the order of the code, which is suitable for scenarios where it is necessary to ensure that tasks are executed in a specific order. By flexibly using these three concepts, developers can give full play to Golang's advantages in high-concurrency scenarios and improve program performance and efficiency.

Question content

I'm learning Go right now and getting really frustrated with the differences between concurrency, parallelism, and sequential.

Suppose we have a process that intercepts fragments of 5 URLs and pastes the contents into a text file. This process takes 2 seconds per URL.

  • Sequentially -> takes 10 seconds because it executes one after the other
  • Parallel -> Takes less than 10 seconds because it performs these operations simultaneously but using multiple threads or processors.
  • Concurrency -> Takes less than 10 seconds but does not require multiple threads or processors.

I'm not sure if I'm right until this point. my question is:

I read that parallelism is doing things at the same time (like running and listening to music), while concurrency is doing things at the same time (like finishing breakfast while ironing your shirt).

But if that's the case, why doesn't concurrency take 10 seconds to complete, because at the end of the day, you're not doing everything at the same time, but only doing a subset of everything until everything is done?

Solution

Here is an analogy to explain.

You need to fry 5 eggs, sunny side up. To cook eggs, you crack them onto the skillet, wait a few minutes, and then remove.

  • The sequential method is to fry the first egg, then the second egg, and so on until you have fried 5 eggs.

  • A parallel approach is to hire 5 cooks, have them each fry an egg, and then wait until they are all done.

  • The parallel method is to cook all 5 eggs yourselfactually. That is, you quickly crack each egg into the pan and then remove each egg when it's ready.

The reason you save time without hiring 5 chefs is that the number of chefs doesn't limit your speed. Poaching an egg takes a few minutes, but only takes up a few seconds of your attention and hands at the beginning and end.

The Go runtime is as smart as modern operating system runtimes. They know that while your thread is waiting to receive a network response, the processor can find other things to occupy its attention.

The big picture of concurrency is not so much the number of processors, but overall resource contention. The execution of tasks requires resources and we cannot use more resources than are available. The processor is a resource, but there is also memory storage, memory bandwidth, network bandwidth, file handles, and so on.

The above is the detailed content of Golang - Concurrency, Parallelism, Sequence. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!