Posts which you will also like.

Tuesday, February 7, 2012

A Tutorial on Java for Solution Of Producer-Consumer Problem


Consumer Producer is a problem related to sharing of data.In this problem producer and consumers operates simultaneously on the shared data.More than one producers produce and more than one consumers consume.So two prevent resource quench we must use thread safety rules and enforce sequential access rather than concurrent access.
DIAGRAM-producer
To illustrate this situation we will use following code snippets :

Money Producer Class:

package com.techy.rajeev;

public class Producer implements Runnable {
    private SyncronizedStack stack;
    private int producerNum;
    public Producer(SyncronizedStack stack,int producerNum) {
        this.stack=stack;
        this.producerNum=producerNum;
    }

    public void run(){
        char c;
        for(int i=0;i<30;i++){
            c=(char)(Math.random()*26+'A');
            stack.push(c);
            System.out.println("Producer"+producerNum+"produced :"+c);
            try{
                Thread.sleep((int)(Math.random()*300));
                
            }catch(InterruptedException e){
                System.out.println("Error");
            }
        }
    }
}





Plate Consumer Class:


package com.techy.rajeev;

public class Consumer implements Runnable{
    private SyncronizedStack stack;
    private int consumerNum;
    public Consumer(SyncronizedStack stack,int consumerNum) {
        this.stack=stack;
        this.consumerNum=consumerNum;
    }

    public void run(){
        char c;
        for(int i=0;i<30;i++){
            c=stack.pop();
            System.out.println("Consumer"+consumerNum+"consumed:"+c);
            try{
                Thread.sleep((int)(Math.random()*300));
            }catch(InterruptedException e){
                System.out.println("Error");
            }
        }
    }

}


>Clock SynchronizedStack Class:

package com.techy.rajeev;

import java.util.Stack;

public class SyncronizedStack {
    private Stack<Character> buffer=new Stack<Character>();
    public synchronized char pop(){
        char c;
        while(buffer.size()==0){
            try{
                this.wait();
            }catch(InterruptedException e){
                
          }
        }
        c=buffer.remove(buffer.size()-1);
        return c;
    }
    public synchronized void push(char c){
        this.notify();
        buffer.add(c);
    }
}


Computer SyncTest Class:


package com.techy.rajeev;

public class SyncTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        SyncronizedStack stack=new SyncronizedStack();
        Producer p1 =new Producer(stack,1);
        Thread t1=new Thread(p1);
        t1.start();
        Producer p2=new Producer(stack,2);
        Thread t2=new Thread(p2);
        t2.start();
        Consumer c1=new Consumer(stack,1);
        Thread ct1=new Thread(c1);
        ct1.start();
        Consumer c2=new Consumer(stack,2);
        Thread ct2=new Thread(c2);
        ct2.start();
    }

}

Thumbs up Output:

Producer1 produced :G
Consumer1 consumed:G
Consumer2 consumed:I
Producer2 produced :I
Producer1 produced :J
Consumer1 consumed:J
Producer2 produced :L
Consumer2 consumed:L
Producer1 produced :E
Consumer1 consumed:E
Consumer1 consumed:G
Producer2 produced :G
Producer1 produced :T
Consumer2 consumed:T
Producer2 produced :I
Consumer2 consumed:I
Producer1 produced :W
Consumer1 consumed:W
Producer2 produced :N
Consumer2 consumed:N
Producer1 produced :J
Consumer1 consumed:J
Producer1 produced :V
Consumer1 consumed:V
Producer2 produced :A
Consumer1 consumed:A
Producer1 produced :E
Consumer1 consumed:E
Producer2 produced :D
Consumer2 consumed:D
Producer2 produced :T
Consumer2 consumed:T
Producer2 produced :I
Consumer2 consumed:I
Producer2 produced :Z
Consumer1 consumed:Z
Producer1 produced :B
Consumer2 consumed:B
Consumer1 consumed:M
Producer2 produced :M
Producer1 produced :N
Consumer2 consumed:N
Consumer1 consumed:K
Producer2 produced :K
Producer1 produced :B
Consumer1 consumed:B
Consumer2 consumed:Y
Producer2 produced :Y
Consumer1 consumed:S
Producer2 produced :S
Consumer2 consumed:P
Producer2 produced :P
Producer1 produced :I
Producer2 produced :W
Producer2 produced :Z
Consumer1 consumed:Z
Producer2 produced :Y
Consumer2 consumed:Y
Producer1 produced :V
Producer1 produced :G
Consumer2 consumed:G
Consumer1 consumed:V
Consumer1 consumed:W
Producer2 produced :K
Consumer1 consumed:K
Consumer1 consumed:I
Producer1 produced :F
Consumer2 consumed:F
........
............
.......................

1 comment:

  1. how to Solve Producer Consumer Problem
    http://www.youtube.com/watch?v=dUwboVZ59KM

    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, who share a common, fixed-size buffer used as a queue

    ReplyDelete

Your comment may wait for moderation....

DMCA.com Protected by Copyscape Online Plagiarism Tool