キュー/ BlockingQueueインターフェースの仕様
序文
通常のキューインターフェイスとブロッキングキューインターフェイスの簡単な紹介
ヒント:以下はこの記事の内容です。以下のケースは参照用です。
1.ソースコード部分をキューに入れる
public interface Queue<E> extends Collection<E> {
/**
* Inserts the specified element into this queue if it is possible to do so
* immediately without violating capacity restrictions, returning
* {@code true} upon success and throwing an {@code IllegalStateException}
* if no space is currently available.
*
* @param e the element to add
* @return {@code true} (as specified by {@link Collection#add})
* @throws IllegalStateException if the element cannot be added at this
* time due to capacity restrictions
* @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 and
* this queue does not permit null elements
* @throws IllegalArgumentException if some property of this element
* prevents it from being added to this queue
*/
boolean add(E e);
/**
* Inserts the specified element into this queue if it is possible to do
* so immediately without violating capacity restrictions.
* 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.
*
* @param e the element to add
* @return {@code true} if the element was added to this queue, else
* {@code false}
* @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 and
* this queue does not permit null elements
* @throws IllegalArgumentException if some property of this element
* prevents it from being added to this queue
*/
boolean offer(E e);
/**
* 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 {@code null} if this queue is empty.
*
* @return the head of this queue, or {@code null} 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 {@code null} if this queue is empty.
*
* @return the head of this queue, or {@code null} if this queue is empty
*/
E peek();
}
-
挿入メソッド:
boolean add(E e);要素をキューに挿入し、失敗した場合はIllegalStateExceptionを返します(キューの容量が十分ではありません)
boolean Offer(E e);キューが追加された場合はtrueを返し、そうでない場合はfalseを返します容量制限がある場合は、オファーを使用してみてください。キューの追加が失敗すると、直接falseが返され、例外処理に追加がスローされるため、時間がかかります。
-
Deleteメソッド:
E remove(); //キューの先頭にある要素を削除し、キューが空の場合は例外をスローします
E poll(); //キューの先頭にある要素を削除し、キューの場合はnullを返します空です -
Findメソッド:
E element(); //キューのhead要素を返し、空の場合は例外をスローします
E peek(); //キューのhead要素を返し、空の場合はnullを返します
キューインターフェイスは、主にエンキューとデキューのいくつかの方法を確認します。これらの方法の違いは、エンキュー操作とデキュー操作が失敗したときの戻り値の違いです。
2、BlockingQueueソースコード部分
public interface BlockingQueue<E> extends Queue<E> {
//与父接口中的add方法类似
boolean add(E e);
//与父接口中的offer方法类似
boolean offer(E e);
/**
*将指定的元素插入到队列中,如果需要等待
*使空间可用。
*
* @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;
//如果队列满了,一直阻塞,直到队列不满了或者线程被中断或者超时
boolean offer(E e, long timeout, TimeUnit unit)
throws InterruptedException;
//如果队列空了,一直阻塞,直到队列不为空或者线程被中断
E take() throws InterruptedException;
//如果队列空了,一直阻塞,直到队列不为空或者线程被中断或者超时
E poll(long timeout, TimeUnit unit)
throws InterruptedException;
//返回剩余容量
int remainingCapacity();
//删除队列中指定元素
boolean remove(Object o);
//队列中是否含有某个元素
public boolean contains(Object o);
//将队列中所有元素删除并添加到指定集合中去
int drainTo(Collection<? super E> c);
//将队列中最多maxElements个元素删除并添加到指定集合中去
int drainTo(Collection<? super E> c, int maxElements);
}
-
入队操作:
void put(E e)はInterruptedExceptionをスローします。
ブール値のオファー(E e、長いタイムアウト、TimeUnitユニット)はInterruptedExceptionをスローします。違いは、オファーがタイムアウトした直後にfalseが返されることです。
-
出队操作:
E take()はInterruptedExceptionをスローします。
Eポーリング(長いタイムアウト、TimeUnit単位)違いは、ポーリングがタイムアウト後にnullを返すことです。