This is a follow up to my previous not answered question, so I try to make it more clear: last question.

I have a n-size buffer that is filled up from an external source, it has 2 main methods get() and add(), get() blocks (via wait()) if buffer is empty and add() blocks if buffer is full.

This buffer object reference is available to multiple consumers (threads).
Each consumer reads data from buffer independently from other consumers .

All Consumers must read ALL data from the buffer.

As someone suggested, I have created A hashmap where key is an id of the consumer and value is the index within the buffer that was read by this consumer.

The problem is that every consumer continues to request data from buffer. I need somehow to block it until next dataitem get available for it in buffer.

Where and how should I block it?
(I can't use wait() in consumers object, because the producer can't call notify() since producer and consumer are different objects)

  • I'm a little confused, I assume you've looked at the various non-blocking queues and concurrent data structures in java.util.concurrent? Commented Dec 19, 2011 at 11:03
  • 1
    This is probably a better question for Stack Overflow rather than Programmers
    – mikera
    Commented Dec 28, 2011 at 14:27

3 Answers 3


Sounds pretty much like Disruptor. There you have a ring buffer with several consumers reading from it. Writer can't write next element until it was read by all consumers and consumer knows where writer is so wont overtake it.

Have a look here for a better explanation than my one :) Source code is available, I heard that there is also C++ implementation of the pattern.



Is performance crucial? Seems like you could use a queue for this instead of implementing it yourself. Perhaps ActiveMQ could help?


I'm not aware of any ready-made data structure that will enable this - the issue is that you need to track the read positions of every consumer so that they all receive their own full copy of the buffered data.

I suspect you are better off using one queue for each reader and have a writer write to all of these queues (in a way that blocks if any of the reader queues are full). I think this will get you the same effect overall.

Also be aware that you get a nasty case when one reader falls behind and all other readers / writers move fast - everything will block on the last reader since the buffer will be holding n items that are only still needed for that reader.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.