I have a Producer/Consumer scenario, where I wan't one Producer to deliver products and multiple Consumers to consume these products. However, the common scenario is that a delivered product is consumed by only one Consumer and the other Consumers never see this specific product. What I wan't to accomplish is that a product is consumed once by each Consumer without any kind of blocking.
My first idea was to use multiple BlockingQueues, one for each Consumer, and make the Producer put each product sequentially in all available BlockingQueues. However, if one of the queues is blocking (e.g., the queue is full), the producer can't go on. On the other hand, spawning a new Thread for each product to put the product in the queue seems to be not a good solution, since if the queue is full, more and more threads will be created.
The problem is, that in my scenario one Consumer could need much more time to process a product than another, depending on the input. It would also be hard to adjust the capacity of the BlockingQueues, since the processing time for a consumer is not really predictable.
Can one think of a more elegant solution? Or is it possible to increase/decrease the capacity of the BlockingQueues dynamically to balance the filling level?
BlockingQueue
, so the problem in your third paragraph shouldn't even be a problem. To be honest, I'm not sure how a singleBlockingQueue
wouldn't suffice... And there areBlockingQueue
s that effectively have unlimited capacity, such as LinkedBlockingQueue – awksp