Producer Consumer Problem
The producer–consumer problem also known as the bounded-buffer problem ,it has two processes the producer and the consumer who share the common fixed size buffer. The producer job is to produce and put into the buffer, at the same time the consumer job is to consume what producer has produced ,one at a time.
The problem is to make sure the producer can not produce if the buffer is full and consumer can not consume if buffer is empty.
The solution to the problem is that, put producer to sleep is buffer is full, now the consumer will consume from buffer and it notify the producer to produce again. In the same way consumer will go for sleep if the buffer is empty, now the producer will produce ,and notify the consumer .
In multi-threading programming environment we use wait() and notify() methods to solve this problem. These methods are also used for inter-thread communication.
Example of Producer-Consumer:
class Que {
int n;
boolean status = false;
synchronized int get(){
while(!status){
try {
wait();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("OBJECT CONSUMED : "+n);
status = false;
notify();
return n;
}
synchronized void put(int n){
while(status){
try {
wait();
} catch (Exception e) {
e.printStackTrace();
}
}
this.n = n;
status = true;
System.out.println("OBJECT PRODUCED : "+n);
notify();
}
}
class Producerr implements Runnable {
Que que;
public Producerr(Que que) {
this.que = que;
new Thread(this,"Producer").start();
}
@Override
public void run() {
int i = 0;
while(i <= 7){ //Here 7 means producer & consumer will go upto 7.
que.put(i++);
}
}
}
class Consumerr implements Runnable{
Que que;
public Consumerr(Que que) {
this.que = que;
new Thread(this,"Consumer").start();
}
@Override
public void run() {
while(true){
que.get();
}
}
}
public class ProducerConsumerExample {
public static void main(String[] args) {
Que q = new Que();
new Producerr(q);
new Consumerr(q);
}
}
Program Output:
OBJECT PRODUCED : 0
OBJECT CONSUMED : 0
OBJECT PRODUCED : 1
OBJECT CONSUMED : 1
OBJECT PRODUCED : 2
OBJECT CONSUMED : 2
OBJECT PRODUCED : 3
OBJECT CONSUMED : 3
OBJECT PRODUCED : 4
OBJECT CONSUMED : 4
OBJECT PRODUCED : 5
OBJECT CONSUMED : 5
OBJECT PRODUCED : 6
OBJECT CONSUMED : 6
OBJECT PRODUCED : 7
OBJECT CONSUMED : 7
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



No comments:
Post a Comment