- Hands-On Full Stack Development with Go
- Mina Andrawos
- 655字
- 2021-07-02 12:33:33
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.
- Dynamics 365 for Finance and Operations Development Cookbook(Fourth Edition)
- 案例式C語言程序設計
- Testing with JUnit
- Python數據可視化:基于Bokeh的可視化繪圖
- Linux核心技術從小白到大牛
- 名師講壇:Java微服務架構實戰(zhàn)(SpringBoot+SpringCloud+Docker+RabbitMQ)
- Effective Python Penetration Testing
- Working with Odoo
- RISC-V體系結構編程與實踐(第2版)
- Learning OpenCV 3 Computer Vision with Python(Second Edition)
- C語言程序設計
- Creating Mobile Apps with jQuery Mobile(Second Edition)
- Fast Data Processing with Spark(Second Edition)
- Machine Learning for OpenCV
- 虛擬現實建模與編程(SketchUp+OSG開發(fā)技術)