public interface BlockingQueue<E> extends Queue<E>
boolean offer(E e); boolean add(E e); When 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接口中添加了方法: java
/** * Inserts the specified element into this queue, waiting if necessary * for space to become available. * * @param e the element to add * @throws InterruptedException if interrupted while waiting * @throws ClassCastException if the class of the specified element * prevents it from being added to this queue * @throws NullPointerException if the specified element is null * @throws IllegalArgumentException if some property of the specified * element prevents it from being added to this queue */ void put(E e) throws InterruptedException; /** * Inserts the specified element into this queue, waiting up to the * specified wait time if necessary for space to become available. * * @param e the element to add * @param timeout how long to wait before giving up, in units of * <tt>unit</tt> * @param unit a <tt>TimeUnit</tt> determining how to interpret the * <tt>timeout</tt> parameter * @return <tt>true</tt> if successful, or <tt>false</tt> if * the specified waiting time elapses before space is available * @throws InterruptedException if interrupted while waiting * @throws ClassCastException if the class of the specified element * prevents it from being added to this queue * @throws NullPointerException if the specified element is null * @throws IllegalArgumentException if some property of the specified * element prevents it from being added to this queue */ boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException;增長了等待加入隊列機制。put會一直等待,而offer則會靈活一些,等待一段時間。
-------------------取數據 this
queue中: spa
/** * Retrieves and removes the head of this queue. This method differs * from {@link #poll poll} only in that it throws an exception if this * queue is empty. * * @return the head of this queue * @throws NoSuchElementException if this queue is empty */ E remove(); /** * Retrieves and removes the head of this queue, * or returns <tt>null</tt> if this queue is empty. * * @return the head of this queue, or <tt>null</tt> if this queue is empty */ E poll(); /** * Retrieves, but does not remove, the head of this queue. This method * differs from {@link #peek peek} only in that it throws an exception * if this queue is empty. * * @return the head of this queue * @throws NoSuchElementException if this queue is empty */ E element(); /** * Retrieves, but does not remove, the head of this queue, * or returns <tt>null</tt> if this queue is empty. * * @return the head of this queue, or <tt>null</tt> if this queue is empty */ E peek();remove,element在queue爲空時會throw exception,poll,peek不會。都是取head的數據。
BlockingQueue中: rest
/** * Retrieves and removes the head of this queue, waiting if necessary * until an element becomes available. * * @return the head of this queue * @throws InterruptedException if interrupted while waiting */ E take() throws InterruptedException; /** * Retrieves and removes the head of this queue, waiting up to the * specified wait time if necessary for an element to become available. * * @param timeout how long to wait before giving up, in units of * <tt>unit</tt> * @param unit a <tt>TimeUnit</tt> determining how to interpret the * <tt>timeout</tt> parameter * @return the head of this queue, or <tt>null</tt> if the * specified waiting time elapses before an element is available * @throws InterruptedException if interrupted while waiting */ E poll(long timeout, TimeUnit unit) throws InterruptedException;會有一個等待的過程,和加數據匹配。
int remainingCapacity();返回理論上隊列中還能存的對象個數。