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

0% completed

What is Multithreading and Concurrency?
Table of Contents

What is Multithreading?

What is Concurrency?

Multithreading vs. Concurrency

What is Multithreading?

Imagine you are sitting in your office and you have multiple tasks to do — reading emails, making phone calls, writing reports, meeting colleagues, and so on. Now, if you were to do these tasks one by one, it would take a good amount of time, right? But what if we could do several of these tasks at once? Like reading an email during a dull meeting (we’ve all been there!), or making a phone call while going through the report. This simultaneous execution of tasks not only makes us more productive but also saves precious time. This is, in the simplest terms, the essence of multithreading.

In computer science, a thread is the smallest sequence of programmed instructions that can be managed independently by a scheduler. Multithreading is the ability of a central processing unit (CPU) (or a single core in a multi-core processor) to provide multiple threads of execution concurrently, supported by the operating system.

To put it in perspective, let’s look at a real-life example. Consider a web browser — it’s a multi-threaded application. When we open multiple tabs, each tab is handled by a separate thread. So, while one tab is loading a webpage, we can continue scrolling or reading on another. This is multithreading at work!

What is Concurrency?

Now that we’ve touched upon multithreading, let’s introduce another intriguing concept — Concurrency.

Imagine you’re cooking a meal. You chop the vegetables, heat the oil, sauté the veggies, and then let them cook. While the veggies are cooking, you start kneading the dough for bread. Here, even though the tasks are related and dependent, you’re not waiting for one to finish completely before starting the next. This ability to overlap tasks is called concurrency.

In the world of computing, concurrency is the execution of the multiple instruction sequences at the same time. It happens in the operating system when there are several process threads running in parallel. The running process threads always communicate with each other through shared memory or message passing. Concurrency results in sharing of resources, in turn making the system efficient.

As an example, let’s consider online banking. When we log in to our account, we can view our balance, make transactions, and maybe even chat with customer support. All these tasks can occur simultaneously and independently. That’s concurrency.

Multithreading vs. Concurrency

Multithreading helps to achieve parallelism in the programs. We can use it to keep the CPU busy and exploit its full potential. For instance, in a game, one thread could handle the game’s graphics, another could handle the user inputs, and yet another could handle the game logic.

Similarly, concurrency helps to interleave the execution of different tasks and maximizes the utilization of the CPU. It allows systems to handle multiple requests and operations, thereby improving throughput and responsiveness. For example, in a web server, each request can be handled independently by a different thread, thus providing concurrent access to multiple users.

Companies are hunting for ways to make things run smoother and quicker. They know users want instant results. And here's the secret: concurrency gives them that edge. By mastering it, you are not just adding a skill but meeting a modern-day need. Understanding concurrency is essential if you want your software to stand out and be on top of the tech game.

Mark as Completed

Table of Contents

What is Multithreading?

What is Concurrency?

Multithreading vs. Concurrency