1 BlockingQueue
在此章節(jié)中肤京,我們會(huì)對(duì)阻塞隊(duì)列進(jìn)行詳細(xì)的介紹。
如果你對(duì)隊(duì)列還不熟悉投队,可以先去看下以下幾篇文章枫疆,或許對(duì)你的入門(mén)有所啟發(fā)!
Java集合--Queue(Java中實(shí)現(xiàn)1)
Java集合--Queue(Java中實(shí)現(xiàn)2)
在講解ArrayBlockingQueue之前敷鸦,我們先來(lái)介紹下它的父類---BlockingQueue息楔。
BlockingQueue
BlockingQueue是一個(gè)接口,是所有阻塞隊(duì)列的父類扒披,定義了阻塞隊(duì)列的主要操作方法值依。
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;
int remainingCapacity();
boolean remove(Object o);
public boolean contains(Object o);
int drainTo(Collection<? super E> c);
int drainTo(Collection<? super E> c, int maxElements);
}
添加方法:
add:插入元素,如果隊(duì)列滿了碟案,拋出異常(底層調(diào)用offer方法)鳞滨;
put:插入元素,如果隊(duì)列滿了蟆淀,就等待拯啦;
offer:插入元素,如果隊(duì)列滿了熔任,就直接返回false褒链;
獲取方法:
element(繼承父類):如果隊(duì)列為空,直接拋出異常(底層調(diào)用peek方法)疑苔;
peek(繼承父類):如果隊(duì)列為空甫匹,則返回null;
移除方法:
remove:移除對(duì)應(yīng)元素,如果隊(duì)列為空兵迅,則返回false;
take:移除元素抢韭,如果隊(duì)列為空,則一直等待恍箭;
poll:移除元素刻恭,如果隊(duì)列為空,則返回null扯夭;
BloeckingQueue成員:
image