Java ArrayBlockingQueue which is an implementation of the BlockingQueue interface was added in Java 5 along with other concurrent utilities like CopyOnWriteArrayList, ReentrantReadWriteLock, Exchanger, CountDownLatch etc.
ArrayBlockingQueue in Java is a bounded blocking queue which internally uses an array to store elements. ArrayBlockingQueue orders elements in FIFO (first-in-first-out) order. When new elements are inserted, those are inserted at the tail of the queue. At the time of retrieval, elements are retrieved from the head of the queue.
Since ArrayBlockingQueue is bounded it means you can't add unlimited number of elements in it. ArrayBlockingQueue has to be created with some initial capacity and that capacity cannot be changed later. 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.
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. A queue created with fairness set to true grants access to waiting producer and consumer threads in FIFO order.
- 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.