What is Concurrency
In this tutorial, we will discuss what concurrency is and how it is different from parallelism. Go language is a concurrent language and not a parallel one.
Concurrency is an ability of a program to do multiple things at the same time. This means a program with two or more tasks that run individually from each other, at about the same time, but remain part of the same program. It’s best explained with an example.
Let us consider a person jogging. During his morning jog, let us say his shoelaces become untied. Now the person stops running, ties his shoelaces and then starts running again. This is a classic example of concurrency.
The person is capable of handling both running and tying shoelaces, that is, the person can deal with lots of things at once 🙂
Large programs are often made up of many smaller sub-programs. For example, a web server handles requests made from web browsers and serves up HTML web pages in response. Each request is handled like a small program.
What is parallelism and how is it different from concurrency?
Concurrency and parallelism come into the picture when you are examining multitasking, and they are often used interchangeably. Concurrent and parallel refer to related but different things.
Parallelism is doing lots of things at the same time. It might sound similar to concurrency, but it’s actually different.
Let us understand it better with the same jogging example. In this case, let’s assume that the person is jogging and listening to music on his mobile. In this case, the person is jogging and listening to music at the same time. That is, he is doing lots of things at the same time. This is called parallelism.
Concurrency and Parallelism – A technical point of view
Up to now, we have understood what concurrency is and how it is different from parallelism using real-world examples. Now let us look at them from a more technical point of view as we are techies.
Let us say we are programming a web browser. The web browser has various components. Two of them are the web page rendering area and the downloader for downloading files from the internet.
Let us assume that we have structured our browser’s code. So that each of these components can be executed independently (This is done using threads in languages such as Java, and in Go language, we can achieve this using Goroutines, more on this later).
When this browser is run in a single-core processor, the processor will context switch between the two components of the browser. It might be downloading a file for some time, and then it might switch to render the html of a user requested web page. This is known as concurrency.
Concurrent processes start at different points of time, and their execution cycles overlap. In this case, the downloading and the rendering begin at different points in time, and their executions overlap.
Let us say the same browser is running on a multi-core processor. In this case, the file downloading component and the HTML rendering component might run simultaneously in different cores. This is known as parallelism.
Parallelism will not always result in faster execution times. This is because the components running in parallel have might have to communicate with each other.
For example, when the file downloading is complete in the case of our browser, this should be communicated to the user, say using a popup.
This communication happens between the component responsible for downloading and the component responsible for rendering the user interface.
Support for concurrency in Go
Concurrency is an inherent part of the Go programming language. Concurrency is handled in the Go language using Goroutines and channels. We will discuss them in detail in the upcoming tutorials.