Java buffer class producer consumer example question

Asked 2 years ago, Updated 2 years ago, 20 views

class buffer
{
    private int data;//cake
    private boolean empty=true;//show cake is empty
    Public synchronized get()//How to buy a cake
    {
        while(empty)
        {
            try {
                wait();
            } } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        empty=true;
        notifyAll();//Wake up the producer
        return data;
    }
    Public synchronized void put(int data) {// Method for producing cakes
        while(!empty)
        {
            try {
                wait();
            } } catch (InterruptedException e) {
                e.printStackTrace();
            }
            empty=false;
            notifyAll();//Wake up consumers
            this.data=data;
        }
    }
}
class Producer extends Thread 
{
    private Buffer buffer;
    public Producer(Buffer buffer)
    {
        this.buffer=buffer;
    }
    public void run()
    {
        for(int i=0;i<=10;i++)
        {
            buffer.put(i);//cake production
            System.out.println ("Producer"+i+"Produce Cake");
            try {
                Thread.sleep(1000);
            } } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
class Cusumer extends Thread 
{
    private Buffer buffer;
    public Cusumer(Buffer buffer)
    {
        this.buffer=buffer;
    }
    public void run()
    {
        for(int i=0;i<=10;i++)
        {
            int data=buffer.get();//consumption
            System.out.println ("Consumers consume "+data+"th cake");
            try {
                Thread.sleep(1000);
            } } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
public class Ex01 {

    public static void main(String[] args) {
        Buffer obj = new Buffer();
        Producer p1 = new Producer(obj);
        Cusumer c1 = new Cusumer(obj);
        p1.start();
        c1.start();

    }

}

``When I produce the first cake with buffer class, the consumer consumes the first cake
Strangely, only producers and consumers are not coming up, so what did I leave out?


java

2022-09-20 19:19

1 Answers

I'll just tell you what the problem is.

You made Producer make a cake in Buffer.put() and Consumer buy a cake in Buffer.get()

As for the flow, Producer thread should produce i first cake in Producer.run() and while resting for 1 second, Consumer thread should buy the first from Consumer.run().

But when I turned on the debug, the consumer kept sleeping because no one woke me up after the first Buffer.get() call. 😴

From Buffer.put() to notifyAll(), it seems that you intended to wake up consumers, but if you look at the code below:

private boolean empty = true;// indicating that the cake is empty

...

while (!empty) {
    ...
    empty = false;

The initial value of the member variable empty is true, so the while statement cannot be entered. So you can't even wake them up.

Then, will the problem be solved if I modify it to enter the while door at first? No.
Both Producer and Consumer do not wake up forever after falling asleep in the wait() part.

Try solving these two problems.

Then I'll turn it off now.

If you're trying to control the timing to wake each other up and sleep, you'd better consider other ways. Timing-affected programming may be affected by the performance of the computer and may not work well.


2022-09-20 19:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.