views:

28

answers:

1

I have three Java's LinkedBlockingQueue instances and I'd like to read from them (take operation) only using one thread. The naive approach is to have one thread per queue.

Is there anything like the UNIX select system call for blocking queues in Java?

Thanks.

+1  A: 

Well, those BlockingQueues were really meant to be serviced by their own Threads.

Something I'd consider trying is to set up a 4th queue for much smaller items, say Booleans, and have the offer() calls on each of the 3 other queues accompany their insertion by inserting a Boolean into that 4th queue. Your thread can then go to sleep on the 4th queue, and when it wakes up it can peek() in the other 3 to find out where to get the goods.

Highly inelegant solution, I think, and I suspect there are possible race conditions where you won't be cleanly woken up some times. But it should basically work.

Carl Smotricz
the 4th queue can be queue of queues. when adding an element into the *ith* queue, add the *ith* queue to the 4th queue. the thread blocked on 4th queue take() will get the *ith* queue which *may* contain a new element.
irreputable
That's a fiendishly clever idea! +1.
Carl Smotricz
Clever indeed. +1
Kapa2ou3