清明節和朋友去被抖音帶火的一個餐廳,下午兩點鐘取晚上的號,前面已經有十幾桌了,四點半餐廳開始正式營業,等輪到咱們已經近八點了。餐廳分爲幾個區域,只有最火的區域(在小船上)須要排號,其餘區域基本上是隨到隨吃的,最冷清的區域幾乎都沒什麼人。菜的價格異常的貴,味道也並很差。最後送出兩張圖:
數組
好了,進入今天的正題,今天要講的是ArrayBlockQueue,ArrayBlockQueue是JUC提供的線程安全的有界的阻塞隊列,一看到Array,第一反應:這貨確定和數組有關,既然是數組,那天然是有界的了,咱們先來看看ArrayBlockQueue的基本使用方法,而後再看看ArrayBlockQueue的源碼。安全
public static void main(String[] args) throws InterruptedException { ArrayBlockingQueue<Integer> arrayBlockingQueue=new ArrayBlockingQueue(5); arrayBlockingQueue.offer(10); arrayBlockingQueue.offer(50); arrayBlockingQueue.add(20); arrayBlockingQueue.add(60); System.out.println(arrayBlockingQueue); System.out.println(arrayBlockingQueue.poll()); System.out.println(arrayBlockingQueue); System.out.println(arrayBlockingQueue.take()); System.out.println(arrayBlockingQueue); System.out.println(arrayBlockingQueue.peek()); System.out.println(arrayBlockingQueue); }
運行結果:
性能
代碼比較簡單,可是你確定會有疑問this
要解決上面幾個疑問,最好的辦法固然是看下源碼,經過親自閱讀源碼所產生的印象遠遠要比看視頻,看博客,死記硬背最後的結論要深入的多。就算真的忘記了,只要再看看源碼,瞬間能夠回憶起來。線程
ArrayBlockQueue提供了三個構造方法,以下圖所示:
3d
public ArrayBlockingQueue(int capacity) { this(capacity, false); }
這是最經常使用的構造方法,傳入capacity,capacity是容量的意思,也就是ArrayBlockingQueue的最大長度,方法內部直接調用了第二個構造方法,傳入的第二個參數爲false。code
public ArrayBlockingQueue(int capacity, boolean fair) { if (capacity <= 0) throw new IllegalArgumentException(); this.items = new Object[capacity]; lock = new ReentrantLock(fair); notEmpty = lock.newCondition(); notFull = lock.newCondition(); }
這個構造方法接受兩個參數,分別是capacity和fair,fair是boolean類型的,表明是公平鎖,仍是非公平鎖,能夠看出若是咱們用第一個構造方法來建立ArrayBlockingQueue的話,採用的是非公平鎖,由於公平鎖會損失必定的性能,在沒有充足的理由的狀況下,是沒有必要採用公平鎖的。視頻
方法內部作了幾件事情:對象
至於排他鎖和兩個條件變量是作什麼用的,看到後面就明白了。blog
public ArrayBlockingQueue(int capacity, boolean fair, Collection<? extends E> c) { //調用第二個構造方法,方法內部就是初始化數組,排他鎖,兩個條件變量 this(capacity, fair); final ReentrantLock lock = this.lock; lock.lock(); // 開啓排他鎖 try { int i = 0; try { // 循環傳入的集合,把集合中的元素賦值給items數組,其中i會自增 for (E e : c) { checkNotNull(e); items[i++] = e; } } catch (ArrayIndexOutOfBoundsException ex) { throw new IllegalArgumentException(); } count = i;//把i賦值給count //若是i==capacity,也就是到了最大容量,把0賦值給putIndex,不然把i賦值給putIndex putIndex = (i == capacity) ? 0 : i; } finally { lock.unlock();//釋放排他鎖 } }
看到這裏,咱們應該明白這個構造方法的做用是什麼了,就是把傳入的集合做爲ArrayBlockingQueuede初始化數據,可是咱們又會有一個新的疑問:count,putIndex 是作什麼用的。
public boolean offer(E e) { checkNotNull(e); final ReentrantLock lock = this.lock; lock.lock();//開啓排他鎖 try { if (count == items.length)//若是count==items.length,返回false return false; else { enqueue(e);//入隊 return true;//返回true } } finally { lock.unlock();//釋放鎖 } }
看到這裏,咱們應該能夠明白了,ArrayBlockQueue是如何保證線程安全的,仍是利用了ReentrantLock排他鎖,count就是用來保存數組的當前大小的。咱們再來看看enqueue方法。
private void enqueue(E x) { final Object[] items = this.items; items[putIndex] = x; if (++putIndex == items.length) putIndex = 0; count++; notEmpty.signal(); }
這方法比較簡單,在代碼裏面就不寫註釋了,作了以下的操做:
這裏就解答了一個疑問:putIndex是作什麼的,就是入隊元素的下標。
public boolean add(E e) { return super.add(e); }
public boolean add(E e) { if (offer(e)) return true; else throw new IllegalStateException("Queue full"); }
這個方法內部最終仍是調用的offer方法。
public void put(E e) throws InterruptedException { checkNotNull(e); final ReentrantLock lock = this.lock; lock.lockInterruptibly();//開啓響應中斷的排他鎖 try { while (count == items.length)//若是隊列滿了,調用notFull的await notFull.await(); enqueue(e);//入隊 } finally { lock.unlock();//釋放排他鎖 } }
能夠看到put方法和 offer/add方法的區別了:
public E poll() { final ReentrantLock lock = this.lock; lock.lock(); try { return (count == 0) ? null : dequeue(); } finally { lock.unlock(); } }
咱們來看dequeue方法:
private E dequeue() { final Object[] items = this.items; @SuppressWarnings("unchecked") E x = (E) items[takeIndex];//得到元素的值 items[takeIndex] = null;//把null賦值給items[takeIndex] if (++takeIndex == items.length)//若是takeIndex自增後的值== items.length,就把0賦值給takeIndex takeIndex = 0; count--; if (itrs != null) itrs.elementDequeued(); notFull.signal();//喚醒由於調用notFull的await方法而被阻塞的線程 return x; }
這裏調用了notFull的signal方法來喚醒由於調用notFull的await方法而被阻塞的線程,那到底在哪裏調用了notFull的await方法呢,還記不記得在put方法中調用了notFull的await方法,咱們再看看:
while (count == items.length) notFull.await();
當隊列滿了,就調用 notFull.await()來等待,在出隊操做中,又調用了notFull.signal()來喚醒。
public E take() throws InterruptedException { final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { while (count == 0) notEmpty.await(); return dequeue(); } finally { lock.unlock(); } }
這裏調用了notEmpty的await方法,那麼哪裏調用了notEmpty的signal方法呢?在enqueue入隊方法裏。
咱們能夠看到take和poll的區別:
public E peek() { final ReentrantLock lock = this.lock; lock.lock(); try { return itemAt(takeIndex); } finally { lock.unlock(); } }
final E itemAt(int i) { return (E) items[i]; }
咱們能夠看到peek和poll/take的區別:
public int size() { final ReentrantLock lock = this.lock; lock.lock(); try { return count; } finally { lock.unlock(); } }
至此,ArrayBlockQueue的核心源碼就分析完畢了,咱們來作一個總結: