官术网_书友最值得收藏!

What is concurrency?

So, what is concurrency? The term is used quite a bit in the software industry, even though not all developers understand its meaning. In this section, we'll attempt to uncover the practical meaning of concurrency from the point of view of the Go language, and why it is useful to you.

In Go, concurrency means the ability of your program to cut itself into smaller pieces, then the ability to run the different independent pieces at different times, with the goal of executing all the tasks as quickly as possible based on the amount of resources available.

The preceding definition might appear (for some people) as though we are defining threads. However, the concept of concurrency is more general than the concept of threads. Let's first briefly define threads if you are not very familiar with the concept.

A thread is a feature that the OS gives you that allows you to run pieces of your program in parallel. Let's say that your program is composed of two main parts, Part 1 and Part 2, and you write your code such that Part 1 runs on Thread One, and Part 2 runs on Thread Two. In this case, both parts of your program will run parallel to each other at the same time; the following diagram illustrates how this will look:

That all sounds good; however, there is a gap in modern software between the number of truly independent threads and the number of concurrent pieces of software that your program needs to execute. In modern pieces of software, you might need thousands of pieces of your program to run independently at the same time, even though your OS might be supplying only four threads!

Concurrency is very important in modern software, due to the need to execute independent pieces of code as fast as possible without disturbing the overall flow of the program. Let's take the simple example of a web server; a web server typically accepts requests from a web client, such as a web browser. Let's say that a request is accepted from Jack, who lives somewhere in Europe, while another request comes at the same time to the web server from Chin, who lives in Asia. You wouldn't want your program to delay Chin's request because of the fact that Jack's request arrived at the same time. Their requests should be processed at the same time and as independently as possible. This is simply why concurrency is an indispensable feature in modern production software.

In Go and other modern programming languages, this gap is addressed by essentially cutting your program into numerous small and independent pieces, and then multiplexing those pieces among the available threads. This becomes much clearer with a visual representation.

Let's say we have a piece of software that is composed of 10 different pieces that we would like to run concurrently, even though we only have two real OS threads. Go has the ability to take those 10 different pieces, schedule when will be the best time to run each piece, and then distribute them among the available threads based on some very clever algorithms. Here's a simple view of how this can look:

The 10 pieces of your program will feel like they are running at the same time, even though, in reality, they were cleverly distributed to make them finish their tasks as soon as possible based on the available resources. Go takes care of all the complexity of scheduling and distributing the 10 pieces of code on the available threads, while providing you with a very clean API that hides away all the complexities of the algorithms involved. This allows you to focus on writing powerful software to serve your needs, without worrying about the low-level concepts such as managing threads, low-level resource allocation, and scheduling.

Let's take a look at goroutines in the next section.

主站蜘蛛池模板: 镇坪县| 井研县| 台安县| 东乡县| 长顺县| 星座| 拜泉县| 金门县| 罗甸县| 元谋县| 廊坊市| 东明县| 海南省| 麦盖提县| 泾源县| 扎鲁特旗| 华容县| 当阳市| 安陆市| 洛阳市| 岳西县| 卓资县| 桃园市| 梓潼县| 宜城市| 萝北县| 镇赉县| 玉田县| 纳雍县| 鹿泉市| 高雄县| 通海县| 湛江市| 广平县| 建水县| 台前县| 澳门| 河间市| 出国| 兴隆县| 永吉县|