1/17
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What is the essence of multithreading? Explain with a real world example
Multitasking is the essence of multithreading. Imagine you are the office. You want to read the requirement doc, while sipping your coffee while attending a meeting.
What is a thread ?
In computer science, a thread is the smallest sequence of programmed instructions that can be managed independently by a scheduler
What is multithreading ?
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.
Give an example of multithreading.
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 ?
In the world of computing, concurrency is the execution of the multiple instruction sequences at the same time. This could happen across threads in a process or across processes. Threads communicate with each other through shared memory, while processes communicate with each other via message passing. Concurrency maximizes CPU efficiency.
Give me an example of concurrency.
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.
What is the essence of concurrency ? Explain with the help of a real world example
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 the essence of concurrency.
Multithreading vs Concurrency . Explain with the help of an example for each.
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.For example, in a web server, each request can be handled independently by a different thread, thus providing concurrent access to multiple users.
Why do we need concurrency and multithreading ? Explain with the help of an example.
Concurrency is the process of interleaving/overlapping execution of a sequence of instructions. Valid across threads and processes.
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.
List the advantages of multithreading.
Enhanced performance and responsiveness
Better resource utilization
Economical and robust
How does multithreading enhance performance and responsiveness ? Explain with the help of an example.
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.
How does multithreading enhance 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.
How is multithreading 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.
What are the advantages of concurrency ?
Improved Throughput and Efficiency
Better CPU Utilization
Concurrent Access for Multiple Users
Usage in Real-Time Applications
How does concurrency improve throughput & efficiency ?
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.
How does concurrency enable multiple users to interact with a system ? Explain with the help of an example.
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.
How does concurrency promote better resource 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.
How is concurrency used in 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.