1public interface BlockingQueue<E> extends Queue<E> 2 3boolean offer(E e); 4boolean add(E e); 5 6 7When using a capacity-restricted queue, this method is generally preferable to {@link #add}, which can fail to insert an element only by throwing an exception.
当使用大小限制的queue的时候,offer要优于add。
上述方法是extends from Queue。在BlockingQueue接口中添加了方法:
1/** 2 * Inserts the specified element into this queue, waiting if necessary 3 * for space to become available. 4 * 5 * @param e the element to add 6 * @throws InterruptedException if interrupted while waiting 7 * @throws ClassCastException if the class of the specified element 8 * prevents it from being added to this queue 9 * @throws NullPointerException if the specified element is null 10 * @throws IllegalArgumentException if some property of the specified 11 * element prevents it from being added to this queue 12 */ 13 void put(E e) throws InterruptedException; 14 15 /** 16 * Inserts the specified element into this queue, waiting up to the 17 * specified wait time if necessary for space to become available. 18 * 19 * @param e the element to add 20 * @param timeout how long to wait before giving up, in units of 21 * <tt>unit</tt> 22 * @param unit a <tt>TimeUnit</tt> determining how to interpret the 23 * <tt>timeout</tt> parameter 24 * @return <tt>true</tt> if successful, or <tt>false</tt> if 25 * the specified waiting time elapses before space is available 26 * @throws InterruptedException if interrupted while waiting 27 * @throws ClassCastException if the class of the specified element 28 * prevents it from being added to this queue 29 * @throws NullPointerException if the specified element is null 30 * @throws IllegalArgumentException if some property of the specified 31 * element prevents it from being added to this queue 32 */ 33 boolean offer(E e, long timeout, TimeUnit unit) 34 throws InterruptedException;
增加了等待加入队列机制。put会一直等待,而offer则会灵活一些,等待一段时间。
-------------------取数据
queue中:
1/** 2 * Retrieves and removes the head of this queue. This method differs 3 * from {@link #poll poll} only in that it throws an exception if this 4 * queue is empty. 5 * 6 * @return the head of this queue 7 * @throws NoSuchElementException if this queue is empty 8 */ 9 E remove(); 10 11 /** 12 * Retrieves and removes the head of this queue, 13 * or returns <tt>null</tt> if this queue is empty. 14 * 15 * @return the head of this queue, or <tt>null</tt> if this queue is empty 16 */ 17 E poll(); 18 19 /** 20 * Retrieves, but does not remove, the head of this queue. This method 21 * differs from {@link #peek peek} only in that it throws an exception 22 * if this queue is empty. 23 * 24 * @return the head of this queue 25 * @throws NoSuchElementException if this queue is empty 26 */ 27 E element(); 28 29 /** 30 * Retrieves, but does not remove, the head of this queue, 31 * or returns <tt>null</tt> if this queue is empty. 32 * 33 * @return the head of this queue, or <tt>null</tt> if this queue is empty 34 */ 35 E peek();
remove,element在queue为空时会throw exception,poll,peek不会。都是取head的数据。
BlockingQueue中:
1/** 2 * Retrieves and removes the head of this queue, waiting if necessary 3 * until an element becomes available. 4 * 5 * @return the head of this queue 6 * @throws InterruptedException if interrupted while waiting 7 */ 8 E take() throws InterruptedException; 9 10 /** 11 * Retrieves and removes the head of this queue, waiting up to the 12 * specified wait time if necessary for an element to become available. 13 * 14 * @param timeout how long to wait before giving up, in units of 15 * <tt>unit</tt> 16 * @param unit a <tt>TimeUnit</tt> determining how to interpret the 17 * <tt>timeout</tt> parameter 18 * @return the head of this queue, or <tt>null</tt> if the 19 * specified waiting time elapses before an element is available 20 * @throws InterruptedException if interrupted while waiting 21 */ 22 E poll(long timeout, TimeUnit unit) 23 throws InterruptedException;
会有一个等待的过程,和加数据匹配。
int remainingCapacity();
返回理论上队列中还能存的对象个数。