How to wait on multiple blocking queues in parallel?

前端 未结 2 1537
青春惊慌失措
青春惊慌失措 2021-01-12 13:00

I have two separated blocking queues. The clients usually use either the first of the second blocking queue to retrieve elements to be processed.

In some case, the c

相关标签:
2条回答
  • 2021-01-12 13:10

    I came across the same problem, and ended up writing my own concurrent queue to support this usage pattern.

    As Java blocking primitived don't allow to block on more than one object, the solution was pushed to the collection itself: Linked Blocking Multi Queue is actually a set set of queues connected by the heads. Elements are offered to individual "sub-queues", but retrieved from a single place (which suppors blocking operations).

    0 讨论(0)
  • 2021-01-12 13:27

    You could try using the poll method in some sort of loop to only wait a specified amount of time for one queue before polling the other one.

    Other than that, I'd say running the blocking operations for each queue on separate threads and providing a callback interface to your main application is another, slightly more complex, option.

    0 讨论(0)
提交回复
热议问题