-2

The problem of this code is in the output. Producer produce more than once. Why and how can I solve it? After each producer should following a consumer. This code can be pasted in IDE and directly run. I think the problem is in the variable ready. I tried to move in different places but it does not work. Please help me.

public class Application
{
    public static void main(String[] args) {
        Buffer b = new Buffer();
        Consumer c1 = new Consumer(b, "C1");
        Consumer c2 = new Consumer(b, "C2");
        Producer p = new Producer(b);
        c1.start();
        c2.start();
        p.start();
        try
        {
            Thread.sleep(5000);
        } catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        p.stop();
        c1.stop();
        c2.stop();
    }
}

class Buffer
{
    protected boolean ready = false;
    private int num;

    public synchronized void put(int x) {
        while (ready)
        {
            try
            {
                wait();
            } catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
        num = x;
        ready = true;
        notifyAll();
    }

    public synchronized int get() {
        while (!ready)
        {
            try
            {
                wait();
            } catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
        ready = false;
        notifyAll();
        return num;
    }
}

class Producer extends Thread
{
    private Buffer buffer;
    private int h;

    public Producer(Buffer b) {
        buffer = b;
    }

    public void run() {
        for (int i = 0; i < 10; i++)
        {
            synchronized (buffer)
            {
                h = (1 + (int) (Math.random() * 10));
                buffer.put(h);
                System.out.println("prodotto:" + h);
            }
        }
    }
}

class Consumer extends Thread
{
    private Buffer buffer;
    private int x;
    private String nome;

    public Consumer(Buffer b, String s) {
        buffer = b;
        nome = s;
    }

    public void run() {
        while (true)
        {
            synchronized (buffer)
            {
                x = buffer.get();
                //C1 C2 prints numbers from 1 to 5
                //C2 prints numbers from 6 to 10
                if (this.nome == "C1" && x < 6)
                {
                    System.out.println(nome + "- consuma:" + x);
                }
                if (this.nome == "C2" && x > 5)
                {
                    System.out.println(nome + "- consuma:" + x);
                }
            }
        }
    }
}

Output>

produced:9
C2- consumed:9
produced:4
C1- consumed:4
produced:4
produced:9
produced:1
produced:9
C2- consumed:9
produced:10
produced:1
produced:3

But after each producer should following a consumer. Is it possible to help me?

Amin
  • 1,643
  • 16
  • 25
David
  • 1
  • 3

1 Answers1

0

the problem is very easy although I didn't understand it at quick look and after 2 or 3 look, I find it. first problem is that you must use equals() for compare Strings.
Second problem which you made is that you check only for numbers greater than 5 only for "C2" and less than 6 only for "C1" and if you have "C2" with greater than or equal to 6, nothing printed (and if you have less than or equal 5 and "C1" nothing printed again) (Challenging for me!!, I think this is multi-threading problem)
There is other mistakes too. You use while(true) for consumer and create 2 cunsumer which create thread which doesn't finish (set them deamon if you want) and prefer use join instead of wait in your main method.

Amin
  • 1,643
  • 16
  • 25