Java blocking queue containing only unique elements

后端 未结 5 1248
长发绾君心
长发绾君心 2020-12-11 15:45

sort of like a \"blocking set\". How can I implement a blocking queue where adding a member that is already in the set is ignored?

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-11 15:55

    You can create a new class that composes a BlockingQueue, a Set, and a lock. When you put() you test against the set while holding a lock that prevents get() from running. When you get() you remove the item from the set so that it can be put() again in the future.

提交回复
热议问题