Java Program to Implement ArrayBlockingQueue API
ArrayBlockingQueue class is a member of the Java Collection framework. ArrayBlockingQueue is a bounded blocking queue. The term bounded, means that the size of the Queue is fixed and cannot be changed. Any attempt to put element/elements into a full queue will lead to blocking operation. Similarly, any attempt to remove element(s) from an empty queue will lead to a blocking operation.
This bound size feature of ArrayBlockingQueue can be achieved by initially passing capacity as the parameter in the constructor of ArrayBlockingQueue. This queue orders elements FIFO (first-in-first-out). It means that elements can be inserted at the tail of the queue and can be removed from the head of the queue.
The tail of the queue has the newest element while the head of the queue has the oldest one.
This class extends AbstractQueue<E> and implements Serializable, Iterable<E>, Collection<E>, BlockingQueue<E>, Queue<E> interfaces.
public class ArrayBlockingQueue<E> extends AbstractQueue<E> implements BlockingQueue<E>, Serializable
// Here, E is the type of elements stored in the collection
- 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.
- clear(): Atomically removes all of the elements from this queue.
- contains(Object o) – Returns true if this queue contains the specified element.
- drainTo(Collection<? super E> c) – Removes all available elements from this queue and adds them to the given collection.
- drainTo(Collection<? super E> c, int maxElements) – Removes at most the given number of available elements from this queue and adds them to the given collection.
- forEach(Consumer<? super E> action) – Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.
- iterator() – Returns an iterator over the elements in this queue in the proper sequence.
- 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.
- 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.
- put(E e) – Inserts the specified element at the tail of this queue, waiting for space to become available if the queue is full.
- remainingCapacity() – Returns the number of additional elements that this queue can ideally (in the absence of memory or resource constraints) accept without blocking.
- remove(Object o) – Removes a single instance of the specified element from this queue, if it is present.
- removeAll(Collection<?> c) – Removes all of this collection’s elements that are also contained in the specified collection (optional operation).
- removeIf(Predicate<? super E> filter) – Removes all of the elements of this collection that satisfy the given predicate.
- retainAll(Collection<?> c) – Retains only the elements in this collection that are contained in the specified collection (optional operation).
- size() – Returns the number of elements in this queue.
- spliterator() – Returns a Spliterator over the elements in this queue.
- toArray() – Returns an array containing all of the elements in this queue, in the proper sequence.
- toArray(T a) – Returns an array containing all of the elements in this queue, in proper sequence; the runtime type of the returned array is that of the specified array.
- take() – Retrieves and removes the head of this queue, waiting if necessary until an element becomes available.
ArrayBlockingQueue (when fair policy is true):[one, two, three, four, five] Peek element of the queue : one Deleting the element 'five' from the queue : true ArrayBlockingQueue :[one, two, three, four] ArrayBlockingQueue after clear operation :
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.