0

My code works well when there is one producer and one consumer. But if i have more than one consumer or producer they got the same value.(I have seen many answers but they are complicated is there any simple solution).

class QC {
int n;
boolean valueSet = false;

synchronized int get() {
    if(!valueSet) {
        try {
            wait();
        } catch (InterruptedException e) {
            System.out.println("InterruptedException caught");
        }
    }
    System.out.println("Got: " + n);
    valueSet = false;
    notify();
    return n;
}

synchronized void put(int n) {
    if(valueSet) {
        try {
            wait();
        } catch (InterruptedException e) {
            System.out.println("InterruptedException caught");
        }
    }
    this.n = n;
    valueSet = true;
    System.out.println("Put: " + n);
    notify();
}
}

Producer

class ProducerC implements Runnable {
QC q;

ProducerC(QC q) {
    this.q = q;
    new Thread(this, "Producer").start();
}

public void run() {
    int i = 0;
    while(true) {
        q.put(i++);
        try {
            Thread.sleep(1000);
        } catch(InterruptedException e) {
            System.out.println("Interrupted");
        }
    }
}
}

Consumer

class ConsumerC implements Runnable {
QC q;

ConsumerC(QC q) {
    this.q = q;
    new Thread(this, "Consumer").start();
}
@Override
public void run() {
    while(true) {
        q.get();
    }
}
}

Main

public class CorrectPC {
public static void main(String[] args) {
    QC q = new QC();
    new ProducerC(q);
    new ConsumerC(q);
    new ProducerC(q);
    System.out.println("Press Control-C to stop.");
}
}
Shuvo
  • 29
  • 1
  • 5
  • What is the result and the expected result? Here, you have two producers, so the single consumer should get every number twice (once from each producer). – Thilo Jan 22 '16 at 06:16
  • Have a look at : http://stackoverflow.com/questions/25393938/multiple-producer-multiple-consumer-multithreading-java – Ravindra babu Jan 22 '16 at 09:39
  • By exactly what metric should this code be simpler than a simple blocking queue? It's longer, untested and handles only a subset of useful functionality (ok I guess that is simpler). – Voo Jan 22 '16 at 12:35

2 Answers2

0

I will use one of the concurrency collections provided by Java to be the common contact point between my producers and consumers. You can find out more from https://docs.oracle.com/javase/tutorial/essential/concurrency/collections.html.

0

Try this.

class QC {
    int n;
    boolean valueSet = false;

    synchronized int get() {
        while (!valueSet) {                     // if -> while
            try {
                wait();
            } catch (InterruptedException e) {
                System.out.println("InterruptedException caught");
            }
        }
        System.out.println("Got: " + n);
        valueSet = false;
        notifyAll();                            // notify -> notifyAll
        return n;
    }

    synchronized void put(int n) {
        while (valueSet) {                      // if -> while
            try {
                wait();
            } catch (InterruptedException e) {
                System.out.println("InterruptedException caught");
            }
        }
        this.n = n;
        valueSet = true;
        System.out.println("Put: " + n);
        notifyAll();                            // notify -> notifyAll
    }
}