Skip to main content

what is Producer Consumer Problem and Solution


Producer-Consumer solution using threads in Java


In computing, the producer–consumer problem (also known as the bounded-buffer problem) is a classic example of a multi-process synchronization problem. The problem describes two processes, the producer and the consumer, which share a common, fixed-size buffer used as a queue.
  • The producer’s job is to generate data, put it into the buffer, and start again.
  • At the same time, the consumer is consuming the data (i.e. removing it from the buffer), one piece at a time.
Problem
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.
Solution 
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.
An inadequate solution could result in a deadlock where both processes are waiting to be awakened.



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.


Uses
 

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


Benefits 

  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 
     

Comments

Popular posts from this blog

Yahoo! Calendar "Add Event" Seed URL Parameters

I can't seem to find any official documentation on this, so here are my notes. Some information gathered from  http://richmarr.wordpress.com/tag/calendar/ Other information gathered through trial and error, and close examination of the "Add Event" form on Yahoo!'s site. Yahoo! Calendar URL Parameters Parameter Required Example Value Notes v Required 60 Must be  60 . Possibly a version number? TITLE Required Event title Line feeds will appear in the confirmation screen, but will not be saved. May not contain HTML. ST Required 20090514T180000Z Event start time in UTC. Will be converted to the user's time zone. 20090514T180000 Event start time in user's local time 20090514 Event start time for an all day event. DUR value is ignored if this form is used. DUR 0200 Duration of the event. Format is HHMM, zero-padded. MM may range up to 99, and is converted into hours appropriately. HH values over 24 hours appear to be modulated by 24. Durations t...

HTML5 30-Day Course Outline

  🗓️ Week 1: HTML5 Fundamentals and Structure 🎯 Goal: Understand the structure of HTML5 and core elements. Day 1: Introduction to HTML5 ✅ Concepts What is HTML5 and its role in web development <!DOCTYPE html> declaration Basic HTML structure ( <html> , <head> , <body> ) Character encoding ( UTF-8 ) ✅ Example Code html <!DOCTYPE html > < html lang = "en" > < head > < meta charset = "UTF-8" > < title >My First HTML5 Page </ title > </ head > < body > < h1 >Hello World! </ h1 > </ body > </ html > ✅ Task: 👉 Create a basic HTML page with a title and h1 element. Day 2: HTML Elements and Semantics ✅ Concepts Block vs Inline Elements <div> , <span> , <article> , <section> , <aside> , <main> , <footer> Importance of semantic HTML for SEO ✅ Example Code html < article > < header > ...

Java KeyWords

Java Keywords Some identifiers are reserved in Java which has separate functionality and meaning such type of reserved identifiers are called reserved words. Java has 54 reserved keywords. We can divide them into the following categories. Primitive types and void : 9 keywords boolean : creates a boolean variable. The only possible values are true and false and the default value is false. byte : creates a byte variable. A byte takes 8-bits and ranges from -128 to 127. char : used to create a character variable. It takes 2-bytes and it’s unsigned. The value ranges from 0 to 65,536. short : create a short variable of 2-bytes. The value ranges from -32,768 to 32,767. int : create an integer variable, takes 4-bytes and the range is from -2,147,483,648 to 2,147,483,647 long : creates a long variable, takes 8-bytes and the range is from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. float : creates a signed floating point variable using 4-bytes. double ...