Java. util. concurrent package source code reading 06 ArrayBlockingQueue, java. util package

Source: Internet
Author: User

Java. util. concurrent package source code reading 06 ArrayBlockingQueue, java. util package

For the specific implementation of BlockingQueue, there are two main points of attention: Implementation of thread security and implementation of blocking operations. Therefore, the analysis of ArrayBlockingQueue is based on these two points.

For thread security, all the methods for adding and removing elements are involved. By analyzing the offer method and poll () method, we can see how thread security is implemented.

First, let's look at the offer method.

    public boolean offer(E e) {        checkNotNull(e);        final ReentrantLock lock = this.lock;        lock.lock();        try {            if (count == items.length)                return false;            else {                insert(e);                return true;            }        } finally {            lock.unlock();        }    }

The Code shows that the Lock is obtained by using the Lock method, then the insert operation is performed, and then the Lock is released.

Therefore, for the poll method, the implementation method must be similar.

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

After talking about the implementation of thread security, let's talk about how blocking is implemented. If you know the wait/notify of the Object, you will understand it very well. This involves an interface called java. util. concurrent. locks. Condition.

Condition has similar operations: await/signal. Condition is related to a Lock, which is created by the newCondition of the Lock. The await method of Condition can be called to wait for notification only when the current thread obtains the lock. Otherwise, an exception is thrown.

Let's take a look at the put method and we will understand how to use a Condition.

notFull =  lock.newCondition();
    public void put(E e) throws InterruptedException {        checkNotNull(e);        final ReentrantLock lock = this.lock;        lock.lockInterruptibly();        try {            while (count == items.length)                notFull.await();            insert(e);        } finally {            lock.unlock();        }    }

The key to implementing blocking is the notFull Condition. When the queue is full, the await method will block the current thread and release the Lock. Wait for other threads to call notFull's signal to wake up the blocked thread. This operation will inevitably appear in the operation of removing elements, so that once an element is removed, the blocked thread will be awakened.

There is a problem here. The thread that sends a signal must have this lock. Therefore, the thread where the await method is located must not be able to obtain this lock, and the await method cannot return immediately, you need to try to obtain the lock until you have the lock before returning it from the await method.

This is the implementation principle of blocking, also known as thread synchronization.

There will also be a notEmpty Condition for the take method.

    public E take() throws InterruptedException {        final ReentrantLock lock = this.lock;        lock.lockInterruptibly();        try {            while (count == 0)                notEmpty.await();            return extract();        } finally {            lock.unlock();        }    }

It should be noted that the lock is also required when the queue length is returned here.

    public E take() throws InterruptedException {        final ReentrantLock lock = this.lock;        lock.lockInterruptibly();        try {            while (count == 0)                notEmpty.await();            return extract();        } finally {            lock.unlock();        }    }

The implementation of ArrayBlockingQueue is relatively simple. It can be done with only one lock. The next article about javasblockingqueue will be much more complicated and two locks are needed.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.