Bounded PriorityBlockingQueue

后端 未结 9 1149
Happy的楠姐
Happy的楠姐 2021-01-01 17:47

PriorityBlockingQueue is unbounded, but I need to bound it somehow. What is the best way to achieve that?

For information, the bounded PriorityBlo

9条回答
  •  灰色年华
    2021-01-01 18:09

    I actually wouldn't subclass it. While I can't put together example code right now, I'd suggest a version of the decorator pattern.

    Create a new class and implement the interfaces implemented by your class of interest: PriorityBlockingQueue. I've found the following interfaces used by this class:

    Serializable, Iterable, Collection, BlockingQueue, Queue
    

    In the constructor for a class, accept a PriorityBlockingQueue as a constructor parameter.

    Then implement all the methods required by the interfaces via the instances of the PriorityblockingQueue. Add any code required to make it Bounded. This is a fairly standard implementation of a Decorator pattern.

提交回复
热议问题