Go Tutorial Series - 20 concurrent entry

Welcome to our  Golang tutorial series on the 20.

Go is a concurrent language, rather than a parallel language . Before discussing how to handle concurrency Go, we must understand what is concurrency, and the difference between concurrent and parallel.

What complicated by that?

Concurrency is the ability to handle multiple tasks immediately. An example can illustrate this point very well.

We can imagine a person is running. If his morning run when, suddenly loose shoelace. So he stopped at the Department of shoelaces, then continue to run. This is a typical example of concurrency. This person can get running and tie them about two things that immediately handle multiple tasks.

What parallel that? What is the difference between parallel and concurrent?

It refers to a plurality of parallel processing tasks simultaneously. It sounds and almost concurrently, but in fact completely different.

We also use this example to help understand the running. If this man while he was jogging, still using his iPod to listen to music. Here, he is listening to music while running, which is the simultaneous processing of multiple tasks. This is called parallel.

From a technical point of view concurrency and parallelism

By real examples, we have to understand what is concurrency, and the difference between concurrent and parallel. As a geek, we are going to examine from a technical point of concurrency and parallelism. :)

If we are writing a web browser. The web browser has various components. Two of which are the downloader web page rendering area and download the file from the Internet. Suppose we've built a browser code, the various components can also be run independently of each other (by a thread like in Java, or through the Go language to be introduced in the  Go coroutines to implement). When the browser running on a single core processor, the processor will context switch between two components browser. It can download files over a period of time, which in turn requested by the user on the web page rendering. This is concurrent. Concurrent process starting from different points in time, alternately run. Here, that is, at different points in time to begin downloading and rendering, and alternating operation.

If the browser is running on a multi-core processor, this time component download files and HTML rendering components may be run simultaneously on different cores. This is called parallel.

image

Parallel will not necessarily run faster, because it may need to communicate between components running in parallel. In our case the browser's, when the file download is complete, it should remind the user, such as a pop-up window. So, between the component responsible for downloading and components responsible for rendering the user interface, it creates communication. On concurrent systems, such communication overhead is small. But in parallel on multi-core systems, communication overhead between the components is high. So, in parallel will not necessarily run faster!

Go support for concurrency

Go programming language native support for concurrency. Use Go  Go coroutine (Goroutines) and a channel (Channel) for concurrent processing. In the next tutorial, we will detail them.

Concurrent presentation ended. Please leave your feedback and comments. wish you happiness.

Guess you like

Origin www.cnblogs.com/fengchuiyizh/p/11349811.html