0

I'm new to java and I'm trying to implement simple producer consumer problem. Below is the code that i've written to test it. I have 3 classes, Main class, producer class and consumer class. Now the problem is my producer is producing the data but my consumer is not consuming it. Could anybody please explain me why this is happening. Thanks in advance.

public class ProducerConsumerWithQueue {
    /**
     * @param args
     */
    public static void main(String[] args) {

        ArrayList<String > queue = new ArrayList<String>();         
        Producer producer = new Producer( queue);           
        Consumer consumer = new Consumer( queue);

        consumer.start();
        producer.start();   
    }
}


public class Producer extends Thread{
    ArrayList<String> queue;
        public Producer(ArrayList<String> queue) {
        this.queue = queue;
    }

    public void run(){
        System.out.println("Producer Started");
        System.out.println("Producer size "+queue.size());
        for(int i=0;i<50;i++){
            try {
                    synchronized (this) {               
                    if(queue.size()>10){
                        System.out.println("Producer Waiting");
                        wait();
                    }else{
                        System.out.println("producing "+i);
                        queue.add("This is "+i);
                        notifyAll();
                    }
                }
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
        }
    }

}

public class Consumer extends Thread{

    ArrayList<String> queue;

    public Consumer(ArrayList<String> queue) {
        this.queue = queue;
    }

    public void run(){
        System.out.println("Consumer started");
        System.out.println("Consumer size "+queue.size());
            try {
                synchronized (this) {               
                    for(int i=0; i>10; i++){
                        if(queue.isEmpty()){
                            System.out.println("Consumer waiting()");
                            wait();

                        }else{
                            System.out.println("Consuming Data "+queue.remove(i));
                            notifyAll();
                        }
                    }
                }
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

    }

}
StKiller
  • 7,631
  • 10
  • 43
  • 56

3 Answers3

5

Your consumer will never run as the for loop does not even run once

for(int i=0; i>10; i++){

Check the i>10 constraint. Probably you want to try i<10

Matthias
  • 3,582
  • 2
  • 30
  • 41
0

what happens when i gets over 10 for the consumer? Assuming you did the synchronization right, the producer is producing 50 elements and the consumer is only going to look at ten of those. also when you do queue.remove(i) how do you know there is an element at that slot? what if

producer runs 1 iteration and inserts element at 0 consumer runs 1 iteration, consumer's i will now be 1 producer runs 1 iteration and inserts element at 0 consumer runs 1 iteration on awake, but cannot consume anything so it waits again.

Might want to reconsider the solution you have :-)

http://www.tutorialspoint.com/javaexamples/thread_procon.htm

0

this is because in your producer you have this :



if(queue.size()>10){
           System.out.println("Producer Waiting");
           wait();
       }

as your produser start first (your consumer waited ) when your producer rich this line it will wait too , and your consumer and producer will be in infinit wait !!! (and this is because you use producer in a 50 loop and so when the produces item reach to 11 the if condition will be true and your producer will wait )

pouyan
  • 3,445
  • 4
  • 26
  • 44