Unsynchronized Producer Consumer Problem

Hi I would like some help implementingan unsynchronized producer consumer with anunbounded queue (implemented using an ArrayList) I have both the producer and consumer sleeping for random amount of time (producer wakes up faster). The problem is that sometimes the producer never gives the consumer a chance to run.

Note I have a notify after I add stuff to the ArrayList. I dont know whether its my choice of collection object or the fact that I have not defined a wait for the producer.

I don't know whether I have explained my problem clearly so feel free to ask some questions. Thanks

[624 byte] By [nube2006a] at [2007-11-26 23:40:38]
# 1

You can't have unsynchronized producer/consumers. If you have multiple threads, you need to synchronize. If you have wait/notify in there, it means you're doing synchronization already (since wait/notify can't be used outside of a synchronized context).

Also if you have only a notify, but no wait, nothing's gonna happen. If nobody is wait()ing on the object you notify, the notify() doesn't really do anything.

Why would you attempt to do an unsynchronized producer/consumer anyway?

-Kayaman-a at 2007-7-11 15:07:22 > top of Java-index,Java Essentials,Java Programming...
# 2
Maybe I should not have used the word unsynchronized. But what I am trying to do is have the producer putting more objects into the buffer than the consumer can remove. The problem is that my producer does not let the consumer run ever.
nube2006a at 2007-7-11 15:07:22 > top of Java-index,Java Essentials,Java Programming...
# 3

It might be helpful if you posted your code.

Also I don't know what context you are implementing this in, but you should look at the java.util.concurrency package. It provides some data structures that may be better then an ArrayList (ConcurrentLinkedQueue, or ArrayBlockingQueue ). Again not knowing what context, have you looked at j.u.c's Executors?

dunnigan14a at 2007-7-11 15:07:22 > top of Java-index,Java Essentials,Java Programming...
# 4

> Maybe I should not have used the word unsynchronized.

> But what I am trying to do is have the producer

> putting more objects into the buffer than the

> consumer can remove. The problem is that my producer

> does not let the consumer run ever.

The basic model is like this:

// Producer

while (!done()) {

synchronized(queue) {

while (queue.isFull()) {

queue.notifyAll();

queue.wait();

}

queue.put(next job);

queue.notifyAll():

}

}

// Consmer

while (!done()) {

synchronized(queue) {

while (queue.isEmpty()) {

queue.notifyAll();

queue.wait();

}

job = queue.get();

queue.notifyAll():

process job

}

}

I expect I've missed a subtlety or two in the whole get/put/wait/notify cycle--I always have to reconstruct it from scratch in my head, it's not something I carry around. It should be pretty close though and should show you the main pieces.

As mentioned, if you use the concurrency package, some of that gunk is hidden from you.

jverda at 2007-7-11 15:07:22 > top of Java-index,Java Essentials,Java Programming...