簡介
ArrayBlockingQueue 字義理解就是 : 數組阻塞隊列;看名字就很好理解!!
ArrayBlockingQueue 是有界隊列,意思是隊列個數不能超出某個數,超出入隊阻塞,初始化就需要定義好個數,不能自動擴增或者修改。
上源碼:
初始化
//初始化,定義臨界值及是否公平兩參數與
public ArrayBlockingQueue(int capacity, boolean fair) {
//臨界值必須>0
if (capacity <= 0)
throw new IllegalArgumentException();
//初始化數組
this.items = new Object[capacity];
lock = new ReentrantLock(fair);
//不空的標誌
notEmpty = lock.newCondition();
//不滿的標誌
notFull = lock.newCondition();
}
入隊
因爲實現BlockingQueue 所以 ArrayBlockingQueue 也有add,put,offer入隊方法
public boolean add(E e) {
//其實就是調用offer 超出個數 報異常
return super.add(e);
}
public boolean offer(E e) {
checkNotNull(e);
final ReentrantLock lock = this.lock;
lock.lock();
try {
//不能超出數組個數
if (count == items.length)
return false;
else {
//入隊
enqueue(e);
return true;
}
} finally {
lock.unlock();
}
}
private void enqueue(E x) {
final Object[] items = this.items;
//直接賦值
items[putIndex] = x;
//如果數組已經滿了 ,重置putIndex 因爲出隊都是從0開始,
if (++putIndex == items.length)
putIndex = 0;
//入隊次數
count++;
//通知不空阻塞 即 我有數據入隊了
notEmpty.signal();
}
public void put(E e) throws InterruptedException {
checkNotNull(e);
final ReentrantLock lock = this.lock;
//可打斷鎖
lock.lockInterruptibly();
try {
//如果 已經滿了,則等待 等通知繼續入隊
while (count == items.length)
notFull.await();
enqueue(e);
} finally {
lock.unlock();
}
}
public boolean offer(E e, long timeout, TimeUnit unit)
throws InterruptedException {
checkNotNull(e);
//獲取超時時間
long nanos = unit.toNanos(timeout);
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
//如果 已經滿了 則等待直到過期,繼續入隊
while (count == items.length) {
if (nanos <= 0)
return false;
nanos = notFull.awaitNanos(nanos);
}
enqueue(e);
return true;
} finally {
lock.unlock();
}
}
出隊
//沒有則返回空
public E poll() {
final ReentrantLock lock = this.lock;
lock.lock();
try {
//如果不空,則出隊
return (count == 0) ? null : dequeue();
} finally {
lock.unlock();
}
}
private E dequeue() {
// assert lock.getHoldCount() == 1;
// assert items[takeIndex] != null;
final Object[] items = this.items;
//直接根據takeIndex 獲取元素 將數組位置賦值爲空
@SuppressWarnings("unchecked")
E x = (E) items[takeIndex];
items[takeIndex] = null;
//超出循環 賦值爲0
if (++takeIndex == items.length)
takeIndex = 0;
//減少個數,對應count++
count--;
if (itrs != null)
itrs.elementDequeued();
//通知不滿條件 因爲出隊 肯定不滿了
notFull.signal();
return x;
}
//必須等到元素出現
public E take() throws InterruptedException {
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
//如果爲空,則 一直等到不空的通知
while (count == 0)
notEmpty.await();
//出隊
return dequeue();
} finally {
lock.unlock();
}
}
//在一定時間內獲取元素,過期返回null
public E poll(long timeout, TimeUnit unit) throws InterruptedException {
//生成時間
long nanos = unit.toNanos(timeout);
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
//如果爲空 則等你個nanos時間,還沒有?返回null
while (count == 0) {
if (nanos <= 0)
return null;
nanos = notEmpty.awaitNanos(nanos);
}
//如果有數據 則出隊
return dequeue();
} finally {
lock.unlock();
}
}
//獲取元素,但不移除,即當前待獲取元素不變
public E peek() {
final ReentrantLock lock = this.lock;
lock.lock();
try {
return itemAt(takeIndex); // null when queue is empty
} finally {
lock.unlock();
}
}
//移除某元素 效率差,儘量避免使用
public boolean remove(Object o) {
if (o == null) return false;
final Object[] items = this.items;
final ReentrantLock lock = this.lock;
lock.lock();
try {
if (count > 0) {
final int putIndex = this.putIndex;
int i = takeIndex;
從//takeIndex 開始循環equals 找到
do {
if (o.equals(items[i])) {
//找到就移除
removeAt(i);
return true;
}
if (++i == items.length)
i = 0;
} while (i != putIndex);
}
return false;
} finally {
lock.unlock();
}
}
//移除某位置元素
void removeAt(final int removeIndex) {
// assert lock.getHoldCount() == 1;
// assert items[removeIndex] != null;
// assert removeIndex >= 0 && removeIndex < items.length;
final Object[] items = this.items;
if (removeIndex == takeIndex) {
// removing front item; just advance
//賦爲null
items[takeIndex] = null;
if (++takeIndex == items.length)
takeIndex = 0;
count--;
if (itrs != null)
itrs.elementDequeued();
} else {
// an "interior" remove
// slide over all others up through putIndex.
final int putIndex = this.putIndex;
//設置putIndex爲removeIndex 即下次插入爲這個位置,並且循環往前移動知道原先的putIndex位置的前一個位置
for (int i = removeIndex;;) {
int next = i + 1;
if (next == items.length)
next = 0;
if (next != putIndex) {
items[i] = items[next];
i = next;
} else {
items[i] = null;
this.putIndex = i;
break;
}
}
//減少個數
count--;
if (itrs != null)
itrs.removedAt(removeIndex);
}
//不滿條件通知
notFull.signal();
}
總結
ArrayBlockingQueue 是有界隊列,不能自動擴容,當時性能相較其他隊列要稍微好點;
適用於確定隊列個數或者某範圍的隊列!