Wk6L1 - Producer-Consumer Flashcards

1
Q

What is the Producer-Consumer Problem?

A

It is a classic synchronization problem where one or more producer threads generate items to add to a shared data structure, and one or more consumer threads remove and process those items.

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

What is the Separation of Concerns in the Producer-Consumer Problem?

A

Producer: Creates tasks (e.g., adding items to a queue).

Consumer: Processes tasks (e.g., removing items from the queue).

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

Why is synchronization necessary for the shared queue in the Producer-Consumer Problem?

A

Synchronization ensures only one thread can add or remove items at a time, preventing issues like adding to a full queue or removing from an empty queue.

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

What is a Blocking Queue in Java?

A

A blocking queue is a thread-safe data structure that handles producer-consumer operations by blocking threads when the queue is full (producers) or empty (consumers).

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

What is the difference between Bounded and Unbounded Blocking Queues?

A

Bounded: Has a fixed size, implemented with arrays; producers must wait if it’s full.

Unbounded: Has no fixed size, implemented with linked lists; risk of data loss if producers can’t be blocked.

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

How do you determine the number of Producer and Consumer Threads?

A

Ideally, the average production rate should be less than or equal to the average consumption rate to prevent overloading the queue.

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

How do you handle shutting down a Producer-Consumer setup?

A

Since blocking queues don’t have a shutdown feature, you can use End of Stream or Poison Objects as markers to signal consumers to stop.

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

What is a Poison Object?

A

A special item added to the queue by producers to indicate no more items are coming. Each consumer checks for this object and terminates when it’s found.

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