Hi everyone. In this video we're going to be talking about concurrency. So take a look at this diagram. Take a look at this one first. So in our analogy, a gopher is a process. And this rectangular block is a CPU code.
So according to this metaphor, one gopher means two minutes of work plus two minutes of break, because they said gophers need their sweet sweet break. So gophers, they come in groups of three. They do their two minutes of work. Each gopher does this two minutes of work, he breaks for two minutes. Then the other gopher comes works for two minute breaks. The other gopher comes work, so two minutes breaks.
And this process keeps on continuing for all of the other processes in our operating system or in any environment, any sandbox environment and then this is what is what is known as a serial task. Now let us take a look at a concurrent task. So you might notice that in our serial task, when a gopher comes over here and works for two minutes And then it takes a break for two minutes and then it leaves, right. So now this arrow is now with the other go for this go for work comes here for two minutes works, takes a break for two minutes and then leaves. Right, then this gopher comes. So in in each of these processes, one thing you might have noticed was that two minutes were getting wasted while a gopher was taking a break.
So converting this to a real life scenario. This is what happens in input and output requests. So if you're talking about input and output request where discretes are required, so let's say you need to watch a movie, you double click on that movie, you open the movie, and while the movie is opening, the CPU is idle, because the because it's already sent out to task to read the contents of the movie from the disk. So while the contents returned, the CPU is idle, it does not have have any task to do. So this is what is called a serial task. Concurrent tasks make use of this idle time in order to get full CPU usage.
So if you're talking about a concurrent task, a gopher is going to come. He's going to work for two minutes. And while this gopher is waiting, another gopher can come work for two minutes. And when his work is done, this gopher comes back again, finishes his work and then goes away. So repeating the whole process by so repeating this whole process, one gopher comes, does his work. Wait for two minutes.
In those two minutes, the other gopher comes, works and then works for two minutes. And then when he is waiting, this gopher comes and then whatever work is left, he does that and then he goes off, then this gopher can come. So the idea is that no time no CPU Your time is wasted because CPU is capable of doing great things. So if we waste our time on waiting for these processes To have occurred or waiting for some process to give us a result back then there is a lot of time where our CPU isn't just doing anything just sitting around idle. So this is called CPU utilization. So one more thing about golang is that it's written in such a way in which it's inherently concurrent, which means it has keywords for concurrency.
And you can have explicit concurrent concurrent threads running. And one more thing about go Lang is that while some say that it's multi threaded, I'd say that it's much more powerful than that. So the golang processes, the threads Don't run. The thread processes don't run on threads, but goroutines are multiplexed on threads. So what it means is, so let this be your CPU core, right? This is our CPU.
And then you have one thread. This is one Right. So, in one third you can have multiple go routines. So, you can say go routine one go routine to go routine three. So, go routines are basically methods which are which are executed concurrently and which can communicate amongst one another using inter process communication. So, goroutines are basically like lightweight threads, and they don't have a one to one relationship with threads rather they are multiplexed into threads, like we've seen over here.
So, they have a lot less memory consumption and cost than invoking a full thread. So, this is how this is what is concurrency and how go is very good at concurrency. And we are going to be talking a bit more about concurrency versus parallelism in the next video. So safe state here.