I have multiple BlockingQueues containing messages to be sent. Is it possible to have fewer consumers than queues? I don’t want to loop over the queues and keep polling them (busy waiting) and I don’t want a thread for every queue. Instead, I would like to have one thread that is awoken when a message is available on any of the queues.
I have multiple BlockingQueues containing messages to be sent. Is it possible to have
Share
One trick that you could do is to have a queue of queues. So what you’d do is have a single blocking queue which all threads subscribe to. Then when you enqueue something into one of your BlockingQueues, you also enqueue your blocking queue on this single queue. So you would have something like:
Then when you get a new work item:
Now your consumers can all block on the producerProducer. I am not sure how valuable this strategy would be, but it does accomplish what you want.