Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

Could anyone kindly share the code snippet of java producer consumer program. The objective of the program is that at a given point of time there should be only 1 item. I know I should be using wait and notify but not able to get the complete snippet. Can this be done without using any kind of data structure?

Thanks in advance.
Posted
Comments
Sergey Alexandrovich Kryukov 18-Aug-13 0:34am    
You cannot expect anyone to do you work for you. And I doubt the pattern makes any sense if only one item is maintained.

As to "without any kind of data structure", it depends on strict formulation of this requirement. Again, I don't see a whole lot of sense in it. Anyway, a data item can be some structure, but it can be a primitive type. And the producer, consumer and other related classes can be considered as non-data. So, what's the sense of it?

—SA
Member 10215848 18-Aug-13 1:09am    
I did my home work FYI... and got the soln with the hint given by some one... Don't think that am just posting ques without even trying out the soln... have posted the soln below.. Just FYI.. everybody are matured enough to do their home work before asking in a forum, what they expect is a hint or direction, if the other person is matured enough either he gives his suggestions or keeps quiet instead of teaching maturity :)
Sergey Alexandrovich Kryukov 18-Aug-13 2:05am    
If so, you should have shown your code with your question, not after. And I cannot agree with your acceptance of your own solution.
Who do you thing try to teach you maturity?! I did not give you more suggestions than I did only because I did not thing you need any more.
—SA

1 solution

Java
package Thread;

class Consumer extends Thread {
    private Good gObj;

    Consumer(Good g) {
        gObj = g;
    }

    public void run() {
        synchronized (gObj) {
            try {
                if(gObj.get().equals(""))
                {
                    System.out.println("Consumer waiting for good to be available");
                    gObj.wait();                                        
                }
                System.out.println("Got a good-->" + gObj.get());
            } catch (InterruptedException e) {

                e.printStackTrace();
            }
        }
    }
}

class Good {
    private String item = "";

    public void set(String str) {
        item = str;
    }

    public String get() {
        return item;
    }
}

public class Producer implements Runnable {
    private Good gObj;

    Producer(Good g) {
        gObj = g;
    }

    public void run() {
        synchronized (gObj)
        {
            if(gObj.get().equals(""))
            {
                System.out.println("Going to produce an item");
                gObj.set("hi");             
                gObj.notify();  
            }           
        }
    }

    public static void main(String[] args) {
        Good g = new Good();
        Producer p = new Producer(g);
        Thread producerThread = new Thread(p);  
        Consumer c = new Consumer(g);
        c.start();
        producerThread.start();
    }
}
 
Share this answer
 
v2
Comments
pasztorpisti 18-Aug-13 10:55am    
-1. This is not a solution to the producer-consumer problem. The solution to the producer-consumer problem is a fixed size blocking queue. Even if we decide the queue size as 1 your solution isn't sufficient as it will never be able to block the producer if the queue is currently full. Your solution correctly demonstrates a single-shot event and not a fixed size blocking queue with a maximum size of 1.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900