In Java, is there a way for a thread to listen to multiple java.util.concurrent.BlockingQueue? Just like java.nio.channels.Selector listen to multiple java.nio.channels.SocketChannel.
Is there a way for a thread to listen to multiple BlockingQueues in Java?
58 views Asked by spin At
1
If you come across a situation like this, I would recommend you to first think about why you need to listen to multiple
BlockingQueues. If possible, try inserting to the sameBlockingQueuein all of the producer tasks. This should be applicable if all consumer tasks are want to listen to all of the queues.You can create another (master)
BlockingQueuethat gets entries from all otherBlockingQueueand one Thread for eachBlockingQueueyou want to listen to that moves data to the masterBlockingQueue:Then, you can read from all queues by reading from
masterQueue.This code makes the assumptions that it's no problem if elements from
toListenFromqueues are removed formasterQueueeven ifmasterQueueis full (these will be inserted later).Due to that, my implementation is not "fair". The threads reading from the
masterQueuewould "reserve" one element from each other queue.