<aside> 📌
Overview: how concurrency works and async in Typescript, Golang, Python, and Rust.
</aside>
In the current consumer computers, every program runs for a specific time slot, and then it stops its execution to let another program continue its execution. This thing runs in a cycle so fast that it's impossible to notice. We think our computers run many programs simultaneously, but this is an illusion (except on multiprocessor machines).
Let’s start with concurrency.
Concurrency means executing multiple tasks at the same time but not necessarily simultaneously. In a concurrent application, two tasks can start, run, and complete in overlapping time periods; for example, Task 2 can start even before Task 1 is completed.
Parallelism
Parallelism is the simultaneous execution of multiple computations or processes. It's a specific form of concurrency where tasks run simultaneously on different processors or cores.
Multithreading
Multithreading is a programming concept where a program can manage its execution in multiple threads. Each thread represents a separate flow of control within the program. Threads can run concurrently and in parallel if the hardware supports it.
Async
Asynchronous programming is a programming paradigm that allows a unit of work to run separately from the primary application thread. When the job is complete, it notifies the main thread about its completion or failure (like Promises and Futures). This is particularly useful for operations that might take a long time to complete, such as I/O operations.
Quick Summary
| Concept | Description |
|---|---|
| Context Switching and Parallelism | Way tasks are executed |
| Synchronous and Asynchronous | Programming model |
| Single Threaded and Multi-Threaded | The environment of task execution |
When you execute something synchronously, you wait for it to finish before moving on to another task. When you execute something asynchronously, you can move on to another task before it finishes.