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! ๐ช๐