Grokking Multithreading and Concurrency for Coding Interviews
Ask Author
Back to course home

0% completed

Why Multithreading and Concurrency Are Essential Today
Table of Contents

Why Do We Need Multithreading?

  1. Enhanced Performance and Responsiveness
  1. Better Resource Utilization
  1. Economical and Robust

Why Do We Need Concurrency?

  1. Improved Throughput and Efficiency
  1. Better CPU Utilization
  1. Concurrent Access for Multiple Users
  1. Real-Time Applications

Concurrency means making your computer do many things at once.

It’s like having many helpers inside your computer, all working together. These helpers are called threads. Each thread acts like a team member, performing their task concurrently.

Now, why is this important? Because as our world moves faster, our tech needs to keep up. No one has time for slow-loading apps. Let's understand this with an example.

Consider a gigantic database: 10 billion entries. Searching for entries using a single thread can be daunting.

Traditional Approach:

  • Data size: 10 billion entries.
  • Time per entry: 1 microsecond.
  • Total time: 10 billion × 1 microsecond = nearly 3 hours of searching.

Wait for almost 3 hours; it sounds tedious.

Concurrency Magic with 100 Threads:

  • Data size: 10 billion entries.
  • Scale: Divide the database into 100 parts, so 100 million entries per thread.
  • Time per entry: Still 1 microsecond.
  • Total Time per thread: 100 million × 1 microsecond = a mere 1.67 minutes.

Result? What took 3 hours now wraps up in under 2 minutes.

Why Do We Need Multithreading?

Imagine you’re trying to watch your favorite show on a streaming platform, and every time the scene changes, the video buffers. Or consider an application that becomes unresponsive when it’s processing a hefty task. How frustrating would that be? Multithreading is the superhero that prevents these situations.

Let’s understand why multithreading is crucial:

1. Enhanced Performance and Responsiveness

Multithreading allows multiple threads of a process to run concurrently, enhancing the performance of a system, especially on multi-core and multi-processor machines. It’s like having a team working on a project instead of an individual — with each member focusing on a particular task, the project gets completed faster.

Further, by allowing tasks to run concurrently, multithreading ensures that the application remains responsive. Even if a thread is busy, other threads can continue their tasks, thus preventing the application from freezing.

For instance, in a word processor, while one thread performs a spell check, another could handle user inputs, preventing any lag in the user experience.

2. Better Resource Utilization

With multithreading, different threads of a single process share the same data space, leading to efficient utilization of resources. Sharing the resources reduces the overhead of memory duplication and synchronization, resulting in improved system productivity.

3. Economical and Robust

Threads are lighter than processes since they share the same address space, resulting in a more economical use of resources. Also, a problem in one thread doesn’t affect the rest of the threads. Hence, it makes the system more robust.

Why Do We Need Concurrency?

Let’s look into why concurrency is an essential aspect:

1. Improved Throughput and Efficiency

Concurrency allows multiple tasks to overlap, leading to enhanced system throughput. This capability enables a system to accomplish more work in the same amount of time.

For instance, an airline reservation system serves multiple customers at the same time. Each customer interacts with the system as if they have their own dedicated system. Behind the scenes, it’s concurrency that enables the system to handle multiple user requests simultaneously.

2. Better CPU Utilization

Concurrency improves CPU utilization by allowing a task to run whenever the CPU has free time, instead of waiting for one task to complete fully before starting another. The interleaving nature of concurrency ensures that the CPU is always kept busy, leading to efficient use of resources.

3. Concurrent Access for Multiple Users

Concurrency enables multiple users to access a shared resource simultaneously. For instance, a database management system allows multiple users to query and modify a database concurrently. Without concurrency, users would have to wait for their turn, leading to delays and dissatisfaction.

4. Real-Time Applications

Concurrency is essential for real-time systems where several tasks need to be executed simultaneously and independently. For instance, in a self-driving car, multiple operations like steering, monitoring the surroundings, and adjusting the speed need to happen concurrently to ensure smooth and safe driving.

In this course, we will begin by understanding the building blocks of modern computing: programs, processes, and threads. These foundational concepts are the bedrock of everything we'll explore in this course, and they are the keys to unlocking the true potential of concurrent and parallel computing.

Mark as Completed

Table of Contents

Why Do We Need Multithreading?

  1. Enhanced Performance and Responsiveness
  1. Better Resource Utilization
  1. Economical and Robust

Why Do We Need Concurrency?

  1. Improved Throughput and Efficiency
  1. Better CPU Utilization
  1. Concurrent Access for Multiple Users
  1. Real-Time Applications