I have a bunch of producer threads adding to a BlockingQueue and one worker thread taking objects. Now I want to extend this so two worker threads are taking objects, but doing different work on the objects. Here’s the twist: I want an object that has been put on the queue to be worked on by both of the receiving threads.
If I keep using BlockingQueue, the two threads will compete for the objects, and only one of the worker threads will get the object.
So I’m looking for something similar to BlockingQueue, but with broadcast behaviour.
The application: The producer threads are actually creating performance measurements, and one of the workers is writing the measurements to a file, while the other worker is aggregating statistics.
I’m using Java 6.
So does such a mechanism exist? In Java SE? Elsewhere? Or do I need to code my own?
I’m looking for solutions with a small footprint – I’d prefer not to install some framework to do this.
One option: have three blocking queues. Your main producer puts items into a “broadcast” queue. You then have a consumer of that queue which consumes each item, putting it into both of the other queues, each of which is serviced by a single consumer:
Alternatively, you could give two blocking queues to the producer, and just get it to put the items it produces into both. That’s less elegant, but slightly simpler overall 🙂