我正在阅读 B. Goetz Java Concurrency In Practice,现在正在阅读有关并发集合的部分。他对Java 5.0中添加的Queue
接口(interface)描述如下:
While you can simulate the behavior of a
Queue
with aList
— in fact,LinkedList
also implementsQueue
—theQueue
classes were added because eliminating the random-access requirements of List admits more efficient concurrent implementations.
看起来有点困惑。我看了LinkedList
接口(interface)并注意到它没有实现 RandomAccess
,但是ArrayList
做。这是非常明显的,因为 LinkedList
提供对其元素的线性时间随机访问。
你不能解释一下他的意思吗?
最佳答案
他指的不是RandomAccess
标记接口(interface),而是随机访问方法,例如get(index)
。实现Queue
时,您无需担心实现这些方法,因为它们可能效率极低,并且可能会阻止您使类达到您想要的性能。
关于java - 了解 Java 中的并发队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34670547/