Golang Concurrency (1) - Introduction to Concurrency

People can only lose when they give up fighting, so long as they insist on fighting, they will not lose.

People, only lose when they give up fighting, as long as they keep fighting, they have not lost yet.

Golang is a concurrent language not a parallel language.

So what is the inextricable relationship between parallelism and concurrency?

What is concurrency, parallelism?

Concurrency is the ability to do a lot of things at once.

1. Explanation 1: Concurrency means that two or more events occur at the same time; while concurrency means that two or more events occur at the same time interval.

    For example: 10 minutes every morning I wash my face, brush my teeth, eat breakfast and many other things, this is concurrency. I'm boiling water to cook while brushing my teeth and that's parallel.

    The key to concurrency is your ability to handle multiple tasks, not necessarily at the same time. The key to parallelism is your ability to handle multiple tasks at the same time.

 

2. Explanation 2: Parallelism is multiple events on different entities, and concurrency is multiple events on the same entity.

 

3. Explanation 3: Multiple tasks are processed "simultaneously" on one processor, and multiple tasks are processed simultaneously on multiple processors. such as hadoop distributed cluster

    Therefore, the goal of concurrent programming is to fully utilize each core of the processor to achieve the highest processing performance.

 

On a technical level:

    If there are two actions of video playback and file download in a web page, when the browser is running on a single-core processor, the CPU core will switch back and forth between these two events, (simultaneously) playing video and downloading, which is called concurrency. Concurrent processes start at different points in time and have overlapping execution cycles.

 What is perceived by the naked eye is simultaneous.

     If your CPU is a multi-core processor, then downloading and playback will be performed on different CPU cores at the same time, which is parallelism.

    Parallelism does not necessarily make execution time faster. This is because components running in parallel may have to communicate with each other. For example, in our browser, when the file download is complete, it should be passed to the user, say using a popup. This communication occurs between the component responsible for downloading and the component responsible for rendering the user interface. This communication overhead is low in concurrent systems. This communication overhead is high when components run in parallel across multiple cores. So parallel programs do not necessarily make execution time faster!

Go supports concurrency

Use goroutines and channels to handle concurrent execution in Golang.

Concurrency is parallelism.

More introductions about concurrency and parallelism can be found in Google Baidu.

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324943555&siteId=291194637