I've realized a (relatively) common activity which has many parallels (ha) to async and threads: Cooking!
Cooking
If you're making a dish, you're not going to first make the sauce, and then the rice, and then the broccoli, and then the carrots, etc. It's just not feasible to do everything sequentially.
There's 2 main ways to solve this:
- Bring in a friend to help, but you gotta wait for them to come over, and there's only so many people you can have help you before you're all just getting in each other's way.
- Start one thing, then let that keep going while you do another. You could start the rice, then make the sauce, then the broccoli and carrots, and then put them all together
Of course, you can put these together, and have both you and your friend start one thing, and then go on to another.
Coding
If you're making a program, you're not going to first query the database, and then ping an API, and then read a file, etc. It's just not feasible to do everything sequentially.
There's 2 main ways to solve this:
- Spawn a thread, but you gotta wait for OS to create it, and there's only so many you can spawn before they all just start hogging each others CPU time.
- Start an async task, then go on to something else. You could send a request to the database, then ping the API, then read a file, and then get the response from them all.
Of course, you can put these together, and have many threads run async tasks.