In this tutorial, we will learn about the ArrayBlockingQueue, its contructors and methods and how to implement it in Java.
Java Collection frameworks provide ArrayBlockingQueue class and it implements BlockingQueue Interface.
ArrayBlockingQueue is a bounded queue of fixed size backed by an array.It orders elements FIFO (First-In-First-Out).
Suppose, one thread is inserting elements to the queue and another thread is removing elements from the queue.Now, if the first thread is slower than the second thread, then the array blocking queue can make the second thread waits until the first thread completes its operations.
Constructors | Description |
---|---|
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. If the fair value is if true then queue accesses for threads blocked on insertion or removal, are processed in FIFO order; if false the access order is unspecified. |
ArrayBlockingQueue(int capacity, boolean fair, Collection 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. If the fair value is if true then queue accesses for threads blocked on insertion or removal, are processed in FIFO order; if false the access order is unspecified. |
Apart from the methods inherited from its parent class/interface ,it has following methods :
Methods | Description |
---|---|
boolean add(E e) | Inserts the specified element at the tail of this queue returning true upon success. |
void clear() | Atomically removes all of the elements from this queue. |
boolean contains(Object o) | Returns true if this queue contains the specified element. |
int drainTo(Collection c) | Removes all available elements from this queue and adds them to the given collection. |
int drainTo(Collection c, int maxElements) | Removes at most the given number of available elements from this queue and adds them to the given collection. |
Iterator iterator() | Returns an iterator over the elements in this queue in proper sequence. |
boolean offer(E e) | Inserts the specified element at the tail of this queue returning true upon success and false if this queue is full. |
boolean 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. |
E peek() | Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty. |
E poll() | Retrieves and removes the head of this queue, or returns null if this queue is empty. |
E 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. |
void put(E e) | Inserts the specified element at the tail of this queue, waiting for space to become available if the queue is full. |
int remainingCapacity() | Returns the number of additional elements that this queue can ideally (in the absence of memory or resource constraints) accept without blocking. |
boolean remove(Object o) | Removes a single instance of the specified element from this queue, if it is present. |
int size() | Returns the number of elements in this queue. |
Spliterator spliterator() | a Spliterator over the elements in this queue. |
E take() | Retrieves and removes the head of this queue, waiting if necessary until an element becomes available. |
Object[] toArray() | Returns an array containing all of the elements in this queue, in proper sequence. |
T[] 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. |
String toString() | Returns a string representation of this collection. |
import java.util.concurrent.ArrayBlockingQueue;
public class CodeMistic
{
public static void main(String[] args)
{
// define capacity of ArrayBlockingQueue
int capacity = 15;
// create object of ArrayBlockingQueue
ArrayBlockingQueue<Integer> abq = new ArrayBlockingQueue<Integer>(capacity);
// add numbers
abq.add(1);
abq.add(2);
abq.add(3);
// print queue
System.out.println("ArrayBlockingQueue:"+ abq);
// remove all the elements
abq.clear();
// print queue
System.out.println("ArrayBlockingQueue:"+ abq);
}
}
Output:
ArrayBlockingQueue:[1, 2, 3]
ArrayBlockingQueue:[]