Choosing a data structure for a variant of producer consumer problem

后端 未结 2 1556
温柔的废话
温柔的废话 2021-01-13 12:03

Right now, I have a queue, with multiple producers and single consumer.

Consumer thread operation is slow. Also, consumer takes element from queue through a peek ope

2条回答
  •  情深已故
    2021-01-13 12:12

    I'd agree with Jon Skeet (+1) in that you need two stores for recording waiting and in-progress items. I would use a LinkedBlockingQueue and have each of your consumers call take() on it. When an element arrives on the queue it will be taken by one of the consumers.

    Recording what is in progress and what is completed would be separate operation. I would maintain a HashSet of all the items that have not yet completed and my producer would first (atomically) add the item to the HashSet of non-completed items and then pop the item on the queue. Once a consumer have finished it's work, it removes the item from the HashSet.

    Your producer can scan the HashSet to determine what is outstanding.

提交回复
热议问题