Browser docs

Producer Consumer

Intent

Producer Consumer Design pattern is a classic concurrency pattern which reduces coupling between Producer and Consumer by separating Identification of work with Execution of Work.

Explanation

Real-world example

Consider a manufacturing process of item, the producer will need to pause the production when manufacturing pipeline is full and the consumer will need to pause the consumption of item when the manufacturing pipeline is empty. We can separate the process of production and consumption which work together and pause at separate times.

In plain words

It provides a way to share data between multiple loops running at different rates.

Wikipedia says

Dijkstra wrote about the case: “We consider two processes, which are called the ‘producer’ and the ‘consumer’ respectively. The producer is a cyclic process that produces a certain portion of information, that has to be processed by the consumer. The consumer is also a cyclic process that needs to process the next portion of information, as has been produced by the producer We assume the two processes to be connected for this purpose via a buffer with unbounded capacity.”

Programmatic Example

Take our Producer and Consumer example from above. Consider we have a Item class that is produced by Producer class and is added to the ItemQueue.

 1public class Producer {
 2
 3  private static final SecureRandom RANDOM = new SecureRandom();
 4
 5  private final ItemQueue queue;
 6
 7  private final String name;
 8
 9  private int itemId;
10
11  public Producer(String name, ItemQueue queue) {
12    this.name = name;
13    this.queue = queue;
14  }
15
16  /**
17   * Put item in the queue.
18   */
19  public void produce() throws InterruptedException {
20
21    var item = new Item(name, itemId++);
22    queue.put(item);
23    Thread.sleep(RANDOM.nextInt(2000));
24  }
25}

Then, we have the Consumer class that takes the item from the item queue.

 1
 2@Slf4j
 3public class Consumer {
 4
 5  private final ItemQueue queue;
 6
 7  private final String name;
 8
 9  public Consumer(String name, ItemQueue queue) {
10    this.name = name;
11    this.queue = queue;
12  }
13
14  /**
15   * Consume item from the queue.
16   */
17  public void consume() throws InterruptedException {
18    var item = queue.take();
19    LOGGER.info("Consumer [{}] consume item [{}] produced by [{}]", name,
20        item.getId(), item.getProducer());
21
22  }
23}

Now, during the manufacturing pipeline, we can instantiate objects from both the Producer and Consumer clasess as they produce and consumer items from the queue.

 1var queue = new ItemQueue();
 2var executorService = Executors.newFixedThreadPool(5);
 3for (var i = 0; i < 2; i++) {
 4
 5    final var producer = new Producer("Producer_" + i, queue);
 6    executorService.submit(() -> {
 7        while (true) {
 8            producer.produce();
 9        }
10    });
11}
12
13for (var i = 0; i < 3; i++) {
14    final var consumer = new Consumer("Consumer_" + i, queue);
15    executorService.submit(() -> {
16        while (true) {
17            consumer.consume();
18        }
19    });
20}

Class diagram

alt text

Applicability

Use the Producer Consumer idiom when

  • Decouple system by separate work in two process produce and consume.
  • Addresses the issue of different timing require to produce work or consuming work