閱讀967 返回首頁    go 阿裏雲 go 技術社區[雲棲]


ArrayBlockingQueue源碼分析

ArrayBlockingQueue是一個有界的阻塞隊列,底層維護的是一個數組。
遵循先進先出FIFO,從尾部插入,從頭部取出。如果隊列已滿,插入操作將阻塞,如果隊列是空的,從隊列裏麵取出元素也會阻塞。

構造方法

/*
 * fair 當多線程同時訪問時,采用公平鎖,還是非公平鎖,默認 false 非公平鎖
 * 
 * 公平鎖:先發出請求的線程將先執行
 * 非公平鎖:哪個線程的請求先執行的順序不確定
*/
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();
    }

put添加元素:如果隊列已滿將阻塞

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();
        }
    }

add添加元素:如果隊列已滿將拋出異常


public boolean add(E e) {
        return super.add(e);
    }

offer添加元素:如果隊列已滿直接返回false, 不會拋出異常

public boolean offer(E e) {
        checkNotNull(e);
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {

        //如果隊列已滿直接返回false
            if (count == items.length)
                return false;
            else {
                enqueue(e);
                return true;
            }
        } finally {
            lock.unlock();
        }
    }

offer添加元素:如果隊列已滿將先等待給定的時間間隔,如果在等待了給定的時間間隔之後還是滿的,則返回false, 不會拋出異常


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();
        }
    }

take取出對應位置的元素,並將該位置的值設置為null, 如果隊列為空,取出元素操作將阻塞

public E take() throws InterruptedException {
        final ReentrantLock lock = this.lock;
        lock.lockInterruptibly();
        try {

        //如果隊列為空,取出元素操作將阻塞
            while (count == 0)
                notEmpty.await();
            return dequeue();
        } finally {
            lock.unlock();
        }
    }

poll取出元素,並將該位置的值設置為null, 如果隊列為空,立刻直接返回null

public E poll() {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            return (count == 0) ? null : dequeue();
        } finally {
            lock.unlock();
        }
    }

poll取出元素,並將該位置的值設置為null, 如果隊列為空,將先等待給定的時間間隔,如果在等待了給定的時間間隔之後還是空的,則返回null


public E poll(long timeout, TimeUnit unit) throws InterruptedException {
        long nanos = unit.toNanos(timeout);
        final ReentrantLock lock = this.lock;
        lock.lockInterruptibly();
        try {
            while (count == 0) {
                if (nanos <= 0)
                    return null;
                nanos = notEmpty.awaitNanos(nanos);
            }
            return dequeue();
        } finally {
            lock.unlock();
        }
    }

peek取出當前位置的元素,不會將其設置為null 與take不同,如果隊列為空將返回null

public E peek() {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            return itemAt(takeIndex); // null when queue is empty
        } finally {
            lock.unlock();
        }
    }

toArray轉換成數組


public Object[] toArray() {
        Object[] a;
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            final int count = this.count;
            a = new Object[count];
            int n = items.length - takeIndex;
            if (count <= n)// 說明putIndex一定在takeIndex與items.length之間
                System.arraycopy(items, takeIndex, a, 0, count);
            else {
                System.arraycopy(items, takeIndex, a, 0, n);
                System.arraycopy(items, 0, a, n, count - n);
            }
        } finally {
            lock.unlock();
        }
        return a;
    }

最後更新:2017-04-01 17:00:39

  上一篇:go 當你發現Bug你會想起誰,你想不想把Bug告訴他(唱起來)
  下一篇:go phantomjs技巧之將html頁麵轉換為pdf文件