Let’s Get Go-ing with Concurrency!
Go has very, very, good support for concurrency. No matter what programming language you are coming from, this is a good time π to sit down and read π because you’re going to learn something new … Unless you’re coming from Erlang in which case you’ve got concurrency down π and could teach me something about it π Though the code examples will still be useful to see how it’s done in Go.
So first things first, Concurrency is not parallelism this is an absolutely necessary talk for any budding gopher by Rob Pike. We’ll find out with the coming lessons that concurrency is unlocked π with the power of goroutines, channels, condition variables and monitors π΅
Second, what’s concurrency? The ability to do tasks without blocking execution of other code. This is an incomplete definition π but we need to start somewhere! Let’s say you have 3 tasks you need to complete: 1. Make dinner π½οΈ 2. Do Laundry π§Ί 3. Watch the latest episode of your favorite show πΊ How do you do all three? π€ Sit in front of a pot of water, wait until it boils, put in some potatoes π₯ and wait for them to get soft? NO! π I don’t know about you, but for me it would look like this:
- Task 1 – Put the pot on the stove with water to boil π§βπ³
- Task 2 – Collect all clothes π π π½ π©³ π into a basket π§Ί
- Task 1 – Pull out ingredients for dinner π₯¦ π π₯ π§ π§ π§ π₯¬
- Task 3 – Turn TV πΊ on and pull up latest episode
- Task 2 – Put clothes in washer π§Ί π§Ό π πΏ
- Task 1 – Watch show until water π§π¨ boils
- Task 1 – Add ingredients to stew π²
- Task 3 – Watch show ποΈ until washer is done
- Task 2 – Move clothes from washer to dryer π¬οΈπ
- Task 3 – Watch show ποΈ until dinner is ready
- Task 1 – Setup dinner π½οΈ
- Task 2 – Pull clothes π§₯ out of β¨οΈ dryer β¨οΈ
- Task 3 – Finish ποΈ watching show
See how all three tasks got done, but none of them were ever blocking the other tasks? It was only me that was switching from each task that made it seem like all three got done at the same time (in parallel), but what actually happened is when I was waiting for something to finish, I didn’t just stand there π§ I picked up another task.
I did not do all my tasks at the same time, I did them when they were ready to move to their next steps – this is concurrency. If I did each one of them at the same time (Let’s say I had two friends come over) then it would be parallelism. We can see that in this case πΌ Having a friend watch water boil and another friend watch the washer until it turns off wouldn’t make the process any quicker
We can see when I was alone dealing with all my tasks, I got them done in the same time it would take for three π§π§π© of us to do each task separately. That’s the difference with concurrency and parallelism; dealing (concurrency) with many things versus doing (parallelism) many things. Concurrency is not parallelism. It allows for parallelism.
And as a final note, many of the lessons will be going into heavy detail around concurrency. There are still plenty of coding examples, but it’s important to recognize the ideas and patterns of concurrency, before you jump in head first π Concurrency is a notoriously difficult topic to handle.
Just because Go makes it incredibly easy to use concurrency, doesn’t mean Go will make concurrency any easier. Meaning, it’s easy to blame your faults on the tool, instead of admitting your depth of knowledge and lack of understanding is the reason the application is failing. After all, you wouldn’t blame a chainsaw βοΈπͺ for cutting the table in half when you tried to cut the butter π§ would you? π You have full knowledge π§ of when and where to use a chainsaw. Now let’s Go πΉ make that a reality with concurrency! πͺπ