目前我正在阅读java中的集合。在我读到的时候,LinkedTransferQueue
是LinkedBlockingQueue
的超集,其put(E e)
方法插入并且对象并返回void,如果需要,将阻塞,直到队列中的空间变为可用。但我没有看到LinkedTransferQueue
的任何构造函数接受按大小绑定它的能力。
那么如何以及何时在LinkedBlockingQueue上调用put
方法会因为我们没有在任何地方为其指定有界大小来阻止它。
我遇到了一些我没有得到的代码。
TransferQueue<Integer> tq = new LinkedTransferQueue<>(); // not bounded.
tq.put(2); // blocks if bounded and full
第2行评论的含义是什么。
感谢。
答案 0 :(得分:3)
尽管LinkedTransferQueue
无法限制容量,但接口TransferQueue
允许实现有界限。
与其他拦截队列一样,TransferQueue 可能可以限制容量。
目前唯一的实现是无界LinkedTransferQueue
,所以这只是措辞问题。即<{1}} 保证不受限制。
答案 1 :(得分:0)
/**
* Creates a {@code LinkedBlockingQueue} with the given (fixed) capacity.
*
* @param capacity the capacity of this queue
* @throws IllegalArgumentException if {@code capacity} is not greater
* than zero
*/
public LinkedBlockingQueue(int capacity) {
if (capacity <= 0) throw new IllegalArgumentException();
this.capacity = capacity;
last = head = new Node<E>(null);
}
这是LinkedBlockingQueue
构造函数代码,它接收一个设置容量的参数。
/**
* Always returns {@code Integer.MAX_VALUE} because a
* {@code LinkedTransferQueue} is not capacity constrained.
*
* @return {@code Integer.MAX_VALUE} (as specified by
* {@link java.util.concurrent.BlockingQueue#remainingCapacity()
* BlockingQueue.remainingCapacity})
*/
public int remainingCapacity() {
return Integer.MAX_VALUE;
}
这是LinkedTransferQueue
类代码,它的容量始终等于Integer.MAX_VALUE
。