Concurrency, Asynchronous Programming and Multithreading

Suryansh Shrivastava
3 min readAug 14, 2023

--

Concurrency, asynchronous programming, and multithreading are closely related concepts, often used to optimise performance and responsiveness in applications.

Let’s delve into their interrelationships:

1. Concurrency

  • Concurrency is a concept where several tasks appear to run simultaneously, but not necessarily in parallel.
  • It’s about dealing with multiple tasks at once, which can be achieved in various ways, including parallelism (using multiple processors), interleaved processing (time-slicing on a single processor), or even distributed processing across multiple machines.
  • It’s about managing access to shared resources, ensuring that data remains consistent, and tasks are executed in an orderly manner.

2. Asynchronous Programming

  • Asynchronous programming is a form of concurrency where tasks start and then move on without waiting for the previous task to finish. This can be achieved using callbacks, promises, futures, events, etc.
  • It’s especially popular in I/O-bound operations (like reading files, making network requests) where tasks don’t need to wait for the operation to complete and can proceed with other operations, improving responsiveness.
  • Asynchronicity doesn’t necessarily imply parallel execution. The tasks may still be executed in a single thread (like in JavaScript’s Node.js), but the system can handle other tasks while waiting for some tasks to complete.

3. Multithreading

  • Multithreading is a form of concurrency where multiple threads (smallest unit of CPU execution) run in parallel on multi-core processors.
  • It allows true parallel execution of tasks.
  • Managing multithreaded applications can be complex due to issues like race conditions, deadlocks, and the need for synchronisation mechanisms.
  • Unlike asynchronous programming, which may or may not use threads, multithreading always involves multiple threads.

How they relate to each other ?

Concurrency vs. Asynchronous Programming : Concurrency is the broader concept, and asynchrony is one way to achieve it. Asynchronous operations might be carried out concurrently (in parallel) or might simply free up the main thread to do other tasks while the asynchronous task is waiting for some resource.

Concurrency vs. Multithreading: Again, concurrency is the overarching concept. Multithreading is one method to achieve concurrency by running multiple threads in parallel. However, concurrency doesn’t always mean tasks run in parallel; it means they’re being managed in a way that they appear to be running at the same time.

Asynchronous Programming vs. Multithreading: Asynchronous programming can be achieved without multithreading (e.g., event-driven systems like Node.js). Conversely, multithreaded systems can be synchronous, where each thread waits for its operations to complete before moving on. However, in many modern systems, asynchrony and multithreading often go hand in hand, with asynchronous operations being offloaded to different threads to achieve parallelism.

In summary, while these terms are related and sometimes used interchangeably, they each have distinct meanings and implications. Understanding the nuances can help in designing efficient and effective systems.

Concurrency can be achieved through various mechanisms, not just asynchronous programming and multithreading. I will be covering those concepts in my next post

--

--

Suryansh Shrivastava

Protocol Engineer & Blockchain Developer | Go | Solidity | Move | Cross-Chain & Interop | Smart Contracts | Defi (AMM, Lending, Derivatives and Stable-coins)