0

I'm learning to implement producer consumer problem using wait and notify methods. I'm using a linked list, and set limit to 20 items for the list. Below is the code:

import java.util.LinkedList;
import java.util.Random;

public class ProducerConsumer {

    private static final int MAX_SIZE = 10;
    LinkedList<Integer> queue = new LinkedList<Integer>();

    public static void main(String[] args) {
        ProducerConsumer pc = new ProducerConsumer();

        Thread prod = new Thread(new Producer(pc.queue));
        Thread cons = new Thread(new Consumer(pc.queue));

        prod.start();
        cons.start();
    }

}

class Producer implements Runnable {

    // Produce until queue is full.
    private LinkedList sharedQueue;

    Producer(LinkedList sharedQueue) {
        this.sharedQueue = sharedQueue;
    }

    public void run() {

        while (true) {
            synchronized (sharedQueue) {

                System.out.println("size : " + sharedQueue.size());
                while (sharedQueue.size() >= 20) {
                    try {
                        sharedQueue.wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                Random random = new Random();
                int i = random.nextInt();
                System.out.println("Produced: " + i);
                sharedQueue.add(i);
                sharedQueue.notifyAll();
            }
        }
    }
}

class Consumer implements Runnable {

    // Produce until queue is full.
    private LinkedList sharedQueue;

    Consumer(LinkedList sharedQueue) {
        this.sharedQueue = sharedQueue;
    }

    public void run() {

        while (true) {
            synchronized (sharedQueue) {
                while (sharedQueue.isEmpty()) {
                    try {
                        sharedQueue.wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                System.out.println("Element consumed : " + sharedQueue.removeFirst());
                sharedQueue.notifyAll();
            }
        }
    }
}

As evident, when the queue size is greater than or equal to 20 (though I doubt that greater condition is even needed), I make the producer thread wait. Also, I printed the size for the list while producing and I never get it greater than 20 in the logs. Still, when I run the above program, I get Java heap space error. Any help would be appreciated on how to modify the above program to work.

gaurav jain
  • 3,119
  • 3
  • 31
  • 48
  • are you sure this program causes an error? it runs fine for me. – Nathan Hughes Aug 22 '15 at 02:25
  • How are you running your code? – Jorge Campos Aug 22 '15 at 02:48
  • @Nathan Yes I've tried running it multiple times. It would run fine for some time (some seconds), but eventually causes java heap space(out of memory) problem and forces me to exit Eclipse. I'm using Eclipse Luna. Does it have to do anything with Eclipse settings maybe? – gaurav jain Aug 22 '15 at 02:58
  • @Jorge, I didn't quite get your question. I'm just running it normally using Eclipse (Run as java application). – gaurav jain Aug 22 '15 at 02:59
  • 1
    You have answered, your are running it with eclipse. So, try to increase heap size and xms and xmx setting of eclipse. EclipseFolder/eclipse.ini (if windows): http://stackoverflow.com/questions/14382932/increase-jvm-max-heap-size-for-eclipse – Jorge Campos Aug 22 '15 at 03:04
  • One relevant suggestion, although you didn't ask for it, is that when you have errors like this you could leverage a tool that comes with JDK. It's called jvisualvm and is available under /bin. Running it and attaching it to the process that's having an issue will give you a lot of information. – KumarM Aug 22 '15 at 04:28
  • or take a heap dump and analyse it. Will easily give u a clearer picture of which/how many objects are being created. – Amm Sokun Aug 22 '15 at 12:37
  • FYI, we don't implement _problems_. At least, I don't _want_ to implement problems, I try to implement _solutions_. "Producer/Consumer" is neither a problem, nor a solution though: It is a _design pattern_. Design patterns are techniques that keep popping up, over and over again, in people's solutions to various problems. – Solomon Slow Aug 22 '15 at 22:42
  • @JorgeCampos Thanks, I saw other posts of similar kind, but what puzzles me is that when the queue size is as small as 20 elements, why there's Java Heap space error in Eclipse. By the way, your suggestion solved the problem. – gaurav jain Aug 24 '15 at 15:17
  • KumarM, Thanks for your suggestion. @jameslarge I mixed it up a bit as I was in hurry. I know it's a design pattern, just wanted to know why it's implementation wasn't working fine. – gaurav jain Aug 24 '15 at 15:21
  • Your machine is a x86 or x64? Another question, you are using JDK or JRE (you I mean eclipse)? – Jorge Campos Aug 24 '15 at 17:41
  • @Jorge Campos My machine is x64, and regarding your second question I don't know how to check it. – gaurav jain Aug 26 '15 at 14:39
  • Go to Window -> Preferences -> Instaled JREs There sniff around you will find it. If you are using java JRE or JAVA JDK. If it is JRE I advise you to set it to your JDK. The JRE have some limitations regarding memory – Jorge Campos Aug 26 '15 at 15:27
  • @JorgeCampos Thanks for the info, I checked, I'm using JDK 1.8.0_11. – gaurav jain Aug 26 '15 at 15:29

0 Answers0