3.11 BlockingQueue
BlockingQueue介面表示阻塞佇列,是Java并發包中阻塞佇列的介面定義規范,阻塞佇列意味著對于該佇列的操作是執行緒安全的,當多個執行緒存放元素進入佇列或者從佇列中取出元素都是執行緒安全的,阻塞佇列的操作和普通佇列沒有區別,主要是加了執行緒安全控制,其作業原理如下圖,

3.11.1 BlockingQueue介面繼承關系
BlockingQueue是繼承自java集合框架中的Queue介面,

3.11.2 BlockingQueue介面原始碼
可以看到BlockingQueue繼承自Queue介面,Queue介面也順便復習下,關于集合框架佇列的講述可以在筆者集合篇.中找到,
public interface BlockingQueue<E> extends Queue<E> {
//佇列中添加元素的方法
boolean add(E e);
boolean offer(E e);
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;
boolean remove(Object o);
//
int remainingCapacity();
public boolean contains(Object o);
int drainTo(Collection<? super E> c);
int drainTo(Collection<? super E> c, int maxElements);
}
drainTo(Collection<? super E> c)方法表示將佇列元素全部移除并且放入集合c中,該方法相當于佇列全部彈出,
public interface Queue<E> extends Collection<E> {
/**
* 添加元素,和offer方法不同的是add方法可能拋出佇列狀態例外
*/
boolean add(E e);
/**
* 添加元素
*/
boolean offer(E e);
/**
* 和poll()方法相同也是取出頭部元素,區別在于佇列為空拋出例外
*/
E remove();
/**
* 頭部取出元素
*/
E poll();
/**
* 和peek()方法相同,獲取佇列頭部元素,區別在于當佇列為慷訓拋出例外
*/
E element();
/**
* 獲取佇列頭部元素,不對佇列進行修改
*/
E peek();
}
BlockingQueue的offer,poll方法實作了多載,可設定等待時間,其余相關操作概率完全一樣,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/259181.html
標籤:其他
上一篇:WebLogic LDAP遠程代碼執行漏洞(CVE-2021-2109)
下一篇:靜態網頁設計——春節
