ArrayBlockingQueue in Java

ArrayBlockingQueue in Java is an implementation of BlockingQueue interface and is part of java.util.concurrent package.

ArrayBlockingQueue is a bounded blocking queue which internally uses an array to store elements. This queue orders elements FIFO (first-in-first-out). The head of the queue is the element that has been on the queue the longest time. The tail of the queue is that element that has been on the queue the shortest time.

Insertion of the new elements happens at the tail of the queue, and the queue retrieval operations obtain elements at the head of the queue.

Bounded blocking queue

ArrayBlockingQueue is a bounded blocking queue in which a fixed-sized array holds elements. Once created, the capacity cannot be changed. Attempts to put an element into a full queue will result in the operation blocking; attempts to take an element from an empty queue will similarly block.

ArrayBlockingQueue is thread-safe

ArrayBlockingQueue implementation is thread-safe. All queuing methods in the class achieve their effects atomically using ReentrantLock internally.

ArrayBlockingQueue does not allow null

ArrayBlockingQueue does not accept null elements. It throws NullPointerException on attempts to add, put or offer a null.

Output

Java ArrayBlockingQueue Constructors

  • ArrayBlockingQueue(int capacity)– Creates an ArrayBlockingQueue with the given (fixed) capacity and default access policy.
  • ArrayBlockingQueue(int capacity, boolean fair)– Creates an ArrayBlockingQueue with the given (fixed) capacity and the specified access policy.
  • ArrayBlockingQueue(int capacity, boolean fair, Collection<? extends E> c)– Creates an ArrayBlockingQueue with the given (fixed) capacity, the specified access policy and initially containing the elements of the given collection, added in traversal order of the collection’s iterator.

ArrayBlockingQueue Java example

BlockingQueue implementations are designed to be used primarily for producer-consumer queues so let’s see an example of producer-consumer using ArrayBlockingQueue.

We’ll create an ArrayBlockingQueue of capacity 1 and use the put and take methods for insertion and retrieval operations respectively. These methods block the current thread indefinitely until the operation can succeed. Since the queue capacity is 1 so the insertion will be blocked until the element in the queue is consumed.

Output

ArrayBlockingQueue class methods

In BlockingQueue implementation like ArrayBlockingQueue methods come in four forms, with different ways of handling operations that cannot be satisfied immediately, but may be satisfied at some point in the future: one throws an exception, the second returns a special value (either null or false, depending on the operation), the third blocks the current thread indefinitely until the operation can succeed, and the fourth blocks for only a given maximum time limit before giving up.

Insertion methods

  • add(E e)– Inserts the specified element at the tail of this queue if it is possible to do so immediately without exceeding the queue’s capacity, returning true upon success and throwing an IllegalStateException if this queue is full.
  • offer(E e)– Inserts the specified element at the tail of this queue if it is possible to do so immediately without exceeding the queue’s capacity, returning true upon success and false if this queue is full.
  • put(E e)– Inserts the specified element at the tail of this queue, waiting for space to become available if the queue is full.
  • offer(E e, long timeout, TimeUnit unit)– Inserts the specified element at the tail of this queue, waiting up to the specified wait time for space to become available if the queue is full.

Removal methods

  • remove()– Retrieves and removes the head of this queue. Throws NoSuchElementException if this queue is empty.
  • poll()– Retrieves and removes the head of this queue, or returns null if this queue is empty.
  • take()– Retrieves and removes the head of this queue, waiting if necessary until an element becomes available.
  • poll(long timeout, TimeUnit unit)– Retrieves and removes the head of this queue, waiting up to the specified wait time if necessary for an element to become available.

Checking queue

  • element()– Retrieves, but does not remove, the head of this queue. Throws NoSuchElementException if this queue is empty.
  • peek()– Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.

List of other important methods

  • clear()– Atomically removes all of the elements from this queue.
  • contains(Object o)– Returns true if this queue contains the specified element.
  • iterator()– Returns an iterator over the elements in this queue in proper sequence.
  • remove(Object o)– Removes a single instance of the specified element from this queue, if it is present.
    size()- Returns the number of elements in this queue.
  • toArray()– Returns an array containing all of the elements in this queue, in proper sequence.

Important points about ArrayBlockingQueue

  1. ArrayBlockingQueue is a bounded blocking queue.
  2. Uses fixed-sized array to hold elements.
  3. ArrayBlockingQueue in Java orders elements in FIFO (first-in-first-out) order.
  4. In ArrayBlockingQueue insertion of the new elements happens at the tail of the queue, and the retrieval of the elements happens at the head of the queue.
  5. ArrayBlockingQueue does not allow null elements.
  6. ArrayBlockingQueue class supports an optional fairness policy for ordering waiting producer and consumer threads. If fairness is set to true, threads are granted access in FIFO order.
  7. ArrayBlockingQueue is thread-safe. It uses ReentrantLock for guarding all access.

That’s all for the topic ArrayBlockingQueue in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.