Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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)

share|improve this question
    
I'm a little confused, I assume you've looked at the various non-blocking queues and concurrent data structures in java.util.concurrent? –  Martijn Verburg Dec 19 '11 at 11:03
1  
This is probably a better question for Stack Overflow rather than Programmers –  mikera Dec 28 '11 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.

http://martinfowler.com/articles/lmax.html
http://code.google.com/p/disruptor/

share|improve this answer

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

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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