Multi Threading Patterns Flashcards

1
Q

What is the Reader-Writer Lock pattern?

A

The Reader-Writer Lock pattern addresses scenarios with frequent read operations and occasional write operations on shared data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are Readers in the context of the Reader-Writer Lock pattern?

A

Readers are threads that access and potentially modify a copy of the shared data without altering the original data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are Writers in the context of the Reader-Writer Lock pattern?

A

Writers are threads that modify the original shared data requiring exclusive access to ensure data integrity.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What challenge does the Reader-Writer Lock pattern address?

A

It efficiently manages concurrent read and write access while avoiding conflicts.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the use cases for the Reader-Writer Lock pattern?

A

It is ideal for situations where read access significantly outweighs write access such as implementing caching systems or managing key-value stores.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the Active Object pattern?

A

The Active Object pattern decouples task submission (request) from task execution (processing) by introducing a layer of active objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the main thread responsible for in the Active Object pattern?

A

The main thread is responsible for handling user interaction and overall application flow.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are Active Objects in the context of the Active Objects pattern?

A

Active Objects act as intermediaries between the main thread and tasks encapsulating task execution logic and managing concurrent processing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What scenarios benefit from the Active Object pattern?

A

It is beneficial for scenarios where tasks need to be submitted asynchronously or delegated for remote processing such as implementing asynchronous processing frameworks or designing distributed systems.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What real-life example illustrates the Active Object pattern?

A

Consider an online movie streaming service where users submit requests (play a movie). The Active Object pattern efficiently processes these requests without affecting the main user interface.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the Barrier Point?

A

A specific point in the program execution where all threads in a group must arrive before any can proceed further.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the Barrier pattern ensure?

A

The Barrier pattern ensures that threads within a group wait at the barrier point until all threads reach that point allowing them to proceed together to the next stage.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the use cases for the Barrier pattern?

A

The pattern is useful for coordinating tasks that require collective actions or data exchange among all threads. Examples include implementing parallel algorithms where all threads must finish processing data before aggregating results and coordinating tasks that depend on the completion of a set of activities by multiple threads such as in scientific simulations running on multiple processors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Can you provide a real-life example of the Barrier pattern?

A

Think of a scientific simulation running on multiple processors (threads). The Barrier pattern ensures all processors finish calculations on one step before moving on to the next step guaranteeing accurate results. It’s like a virtual checkpoint in the race ensuring all participants (threads) arrive before the next leg of the simulation begins.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the Producer-Consumer pattern?

A

The Producer-Consumer pattern involves two types of threads: producers that generate data and consumers that process it. They communicate through a shared buffer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the use cases of the Producer-Consumer pattern?

A

The use cases include:
- Processing data in batches (e.g. downloading large files in chunks and processing them piece by piece).

  • Image/video processing (pre-processing images for a website while the main thread displays content).
  • Task queues (handling user requests asynchronously to prevent the main thread from blocking).
17
Q

Can you provide real-life examples of the Producer-Consumer pattern?

A

Imagine a bakery with a baker (producer) making bread and a salesperson (consumer) selling it. They don’t directly interact; the baker puts bread on a tray (shared queue) and the salesperson takes bread when needed.

18
Q

What is the Thread Pool pattern?

A

The Thread Pool pattern maintains a pool of pre-created worker threads ready to tackle tasks. This avoids the overhead of creating and destroying threads for each short-lived job maximizing efficiency.

19
Q

What are the use cases of the Thread Pool pattern?

A

The use cases include:

  • Handling network requests (efficiently processing incoming requests from multiple clients concurrently).
  • Background tasks (offloading non-critical tasks like image resizing or data compression to the thread pool without impacting the main program’s responsiveness).
  • Batch processing (executing numerous short-lived tasks in parallel such as sending emails or updating databases).
20
Q

Can you provide real-life examples of the Thread Pool pattern?

A

Here are two examples: - Picture a car wash with a team of prepped car washers (thread pool). Cars (tasks) arrive and an available washer cleans them. This avoids creating new washers for every car saving time. - Imagine a computer repair shop with a team of technicians (thread pool) ready to assist customers (tasks). Customers arrive and an available technician helps them avoiding the need to hire new technicians for every customer.