Java CircularFifoQueue 的性能

Performance of the Java CircularFifoQueue

我有点难以理解 CircularFifoQueue Class 的工作原理。所以根据我的要求,我需要一个固定大小的 FIFO 队列(大约 6000 个元素)。起初我使用的是 ArrayDequeue,但它的性能相当糟糕。然后我阅读了 CircularFifoQueue 并尝试了它。我可以看到性能有所提升,但速度仍然不快。

我现在的问题是:如果队列已满并且我添加一个元素会发生什么?是否复制了整个底层数组?是否有一些偏移量,将被设置,例如

  head = (head + 1) % size;

如果是后者,那我估计是我的算法性能不好。

谢谢!

docs 说了以下关于在 CircularFifoQueue 中插入的内容:

If the queue is full, the least recently added element is discarded so that a new element can be inserted.

说到性能,需要注意的是除了addremovepeekpolloffer方法在常数时间内执行,此数据结构的所有方法在线性时间内执行,或更糟。