Java. util. concurrent package source code reading 05 BlockingQueue, java. util package

Source: Internet
Author: User

Java. util. concurrent package source code reading 05 BlockingQueue, java. util package

Everyone must be familiar with the producer-consumer queue. The producer is responsible for adding elements to the queue. If the queue is full, it will be blocked until a consumer takes the elements away. On the contrary, the consumer is responsible for removing elements from the queue. If the queue is empty, it will be blocked until a producer adds elements to the queue. BlockingQueue is such a producer-consumer queue.

 

BlockingQueue is a sub-interface of Queue.

public interface BlockingQueue<E> extends Queue<E>

When BlockingQueue takes away elements, if the queue is empty, there are two scenarios for blocking wait:

One is to wait until the queue is not empty. In this case, the take method is called.

E take() throws InterruptedException;

The other is to set a time-out period and wait until the time-out period expires. In this case, the pool method is called.

E poll(long timeout, TimeUnit unit) throws InterruptedException;

There are two situations for adding elements:

Always waiting for the put Method

void put(E e) throws InterruptedException;

Use the offer method for timeout wait

boolean offer(E e, long timeout, TimeUnit unit)        throws InterruptedException;

 

The parent interface Queue of BlockingQueue has two interfaces for removing elements: remove and pool.

The difference between the two is that when the queue is empty, the former throws NoSuchElementException, while the latter returns null.

E remove();E poll();

There are also two interfaces for adding elements: add and offer. The difference between the two is that when the queue is full, the former throws IllegalStateException, while the latter returns false.

boolean add(E e);boolean offer(E e);

 

Generally, the data structure of the Queue type has two implementations: array and linked list. Corresponding to BlockingQueue, ArrayBlockingQueue and LinkedBlockingQueue are implemented based on AbstractQueue.

public class ArrayBlockingQueue<E> extends AbstractQueue<E>        implements BlockingQueue<E>, java.io.Serializable
public class LinkedBlockingQueue<E> extends AbstractQueue<E>        implements BlockingQueue<E>, java.io.Serializable

 

Here it is necessary to talk about AbstractQueue. AbstractQueue only implements the add and remove methods, and it is interesting that both methods are implemented by using their corresponding non-exception version methods offer and pool.

    public boolean add(E e) {        if (offer(e))            return true;        else            throw new IllegalStateException("Queue full");    }
    public E remove() {        E x = poll();        if (x != null)            return x;        else            throw new NoSuchElementException();    }

The advantage of doing so is undoubtedly to provide good scalability, that is, to leave the implementation of truly adding/removing elements to the subclass (thread security and non-thread security versions can be implemented ).

 

The focus of research on BlockingQueue is that if Blocking is implemented, the next two articles will detail how ArrayBlockingQueue and LinkedBlockingQueue implement thread Blocking.

 




Related Article

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.