在 Java 中了解并发队列

Understanding Queue for concurrency sakes in Java

我正在阅读 B. Goetz Java 并发实践,现在我正在阅读有关并发集合的部分。他是这样描述Java 5.0中新增的Queue接口的:

While you can simulate the behavior of a Queue with a List — in fact, LinkedList also implements Queue—the Queue classes were added because eliminating the random-access requirements of List admits more efficient concurrent implementations.

好像有点糊涂了。我看了看 LinkedList interface and noticed that it doesn't implement RandomAccess, but ArrayList 呢。这很明显,因为 LinkedList 提供对其元素的线性时间随机访问。

你不能解释一下他的意思吗?

他指的不是RandomAccess标记接口,而是get(index)等随机访问方式。实施 Queue 时,您无需担心实施这些方法,因为它们可能效率极低,并且可能会阻止您使 class 达到您想要的性能。