/** * Adds the specified element as the tail (last element) of this list. * * @param e the element to add * @return {@code true} (as specified by {@link Queue#offer}) * @since 1.5 */ public boolean offer(E e) { return add(e); }这段是offer方法的源码,jdk1.5之后跟直接使用add是无什么区别的。不要尽信书,有时也不必去验证书上的每个理论,注重实际使用。如果你要指定队列的大小,满了之后要执行相应的逻辑,可以使用楼上的方法,cache到异常执行相应的操作就行。
上面,当加第四个元素"efa"时,在例行检查队列长度时,发现队列已满,抛异常
Queue<String> list = new LinkedBlockingQueue<String>(2);
* Adds the specified element as the tail (last element) of this list.
*
* @param e the element to add
* @return {@code true} (as specified by {@link Queue#offer})
* @since 1.5
*/
public boolean offer(E e) {
return add(e);
}这段是offer方法的源码,jdk1.5之后跟直接使用add是无什么区别的。不要尽信书,有时也不必去验证书上的每个理论,注重实际使用。如果你要指定队列的大小,满了之后要执行相应的逻辑,可以使用楼上的方法,cache到异常执行相应的操作就行。