Code Pumpkin

Producer Consumer Design Pattern

April 5, 2017
Posted by Abhi Andhariya

Producer Consumer Problem is a classical concurrency problem. In fact it is one of the concurrency design pattern.  It is also known as the bounded-buffer problem.

The problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue.

The producer's job is to generate data and put it into the buffer. At the same time, the consumer consumes the data (i.e., by removing it from the buffer), one piece at a time.

The problem is to make sure that the producer won't try to add data into the buffer if it's full and that the consumer won't try to remove data from an empty buffer.

The solution for the producer is to either go to sleep or discard data if the buffer is full. The next time the consumer removes an item from the buffer, it notifies the producer, who starts to fill the buffer again. 

In the same way, the consumer can go to sleep if it finds the buffer to be empty. The next time the producer puts data into the buffer, it wakes up the sleeping consumer.

Real Life Example

Lets say in our Example, The producer is Mr Simpson whose job is to bake Pizza and consumer is Joey Tribbiani who consumes Pizza at the same time. Both of them share common Pizza counter to interact with each other.

The problem is to make sure that the Simpson won't try to add more pizza on Pizza counter if it's full. He needs to wait until Joey consumes Pizza.

Similarly Joey can't consume pizza from an empty counter. He needs to wait until Mr Simpson adds Pizza on counter.

You can understand it better by watching this video.


In Java library, Executor framework implements Producer Consumer design pattern to separate responsibility of addition and execution of task.


  1. It simplifies developement process. you can Code Producer and Consumer independently and Concurrently, they just need to know shared object.
  2. Producer doesn't need to know about who is consumer or how many consumers are there. Same is true with Consumer.
  3. Producer and Consumer can work with different speed. In fact by monitoring consumer speed, one can introduce more consumer for better utilization.

Java Program Implementation

Producer Consumer Design Patter can be implemented using following two approaches.

  1. By using wait(), notify() and notifyAll() methods
  2. By using BlockingQueue 

That's all for this topic. If you guys have any suggestions or queries, feel free to drop a comment. We would be happy to add that in our post. You can also contribute your articles by creating contributor account here.

Happy Learning 🙂

If you like the content on CodePumpkin and if you wish to do something for the community and the planet Earth, you can donate to our campaign for planting more trees at CodePumpkin Cauvery Calling Campaign.

We may not get time to plant a tree, but we can definitely donate ₹42 per Tree.

About the Author

Surviving Java Developer, Passionate Blogger, Table Tennis Lover, Bookworm, Occasional illustrator and a big fan of Joey Tribbiani, The Walking Dead and Game of Thrones...!!

Tags: , , , , ,

Comments and Queries

If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code class="java"> 
String foo = "bar";
For more information on supported HTML tags in disqus comment, click here.
Total Posts : 124
follow us in feedly

Like Us On Facebook