java – 为什么PriorityQueue不像队列那样?

前端之家收集整理的这篇文章主要介绍了java – 为什么PriorityQueue不像队列那样?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用PriorityBlockingQueue和优先级字段.在我的测试中,我使用System#currentTime()作为优先级 – 计算机获得的相同优先级是如此之快,以至于毫秒是相同的(或更像是PC上的毫秒具有误差幅度).

当优先级相同时,队列就像堆栈一样,这看起来很奇怪.当元素的优先级相同时,是否有一种替代方法可以使队列像正常队列一样(即FIFO而不是LIFO行为)?

解决方法

@H_404_8@

Operations on this class make no guarantees about the ordering of elements with equal priority. If you need to enforce an ordering,you can define custom classes or comparators that use a secondary key to break ties in primary priority values.

PriorityBlockingQueue docs themselves告诉你这个,以及如果你需要如何绕过它.

猜你在找的Java相关文章