ArrayBlockingQueue in Java

Java ArrayBlockingQueue

A bounded blocking queue supported by an array is the ArrayBlockingQueue in Java class. Bounded implies that the Queue’s size is fixed.

The blocking queue approach utilising an array is provided by the Java Collections framework’s ArrayBlockingQueue class. The capacity argument in the function Object() { [native code] } of the ArrayBlockingQueue can be initially bypassed to obtain the boundness of the queue.

Blocking Queue in java

ArrayBlockingQueue in Java

The concurrent and constrained blocking queue implementation in Java is the ArrayBlockingQueue class, which is supported by an array. The elements are ordered first-in-first-out (FIFO).

The Java Collections Framework class ArrayBlockingQueue implements every optional method of the Collection and Iterator interfaces.

ArrayBlockingQueue in Java

ArrayBlockingQueue Constructures in Java

  • With the specified (fixed) capacity and the default access policy, ArrayBlockingQueue(int capacity) creates an empty queue.
  • The function ArrayBlockingQueue(int capacity, boolean fair) creates an empty queue with the specified access policy and the given (fixed) capacity. Queue accesses for threads stopped on insertion or removal are handled in FIFO order if the fair value is true; if false, the access order is undetermined.
  • ArrayBlockingQueue(int capacity, boolean fair, Collection c) creates a queue with the specified access policy and starting elements from the specified collection, added in the iterator’s traversal order. The queue has a fixed capacity.

Methods of ArrayBlockingQueue in Java

MethodDescription
add()If the supplied element gets inserted at the end of this queue, this method returns the Boolean value true.
clear()The elements in this queue are all automatically removed using this procedure.
contains()This method returns a Boolean value true if this queue contains the specified element.
drainTo()This method clears the queue of all elements, up to the specified maximum number, and adds them to the collection.
forEach()The action is carried out using this technique for each element until each element has been dealt with.
iterator()An iterator over the elements in the correct order is returned by this method.
offer()If the queue is full, this method waits up to the provided wait period for space to become vacant before inserting the supplied element at the tail of the queue.
peak()This approach fetches the queue’s head without taking it out.
poll()The head of this queue is retrieved and eliminated using this technique.
put()This method inserts the specified element at the tail of this queue.
remainingCapacity()The capacity of the elements that this queue can accept without blocking is returned by this method.
remove()If the provided element is already in the queue, this procedure removes it.
removAll()The elements of this in the queue that are part of the specified collection are all removed by this method.
removeIf()This method removes all the elements in this queue that satisfy the given predicate filter.
retainAll()Only the entries of this queue that are part of the provided collection are kept by this procedure.
size()The number of elements in this queue is returned by this method.
spliterator()A spliterator over the elements in this queue is the result of this technique.
toArray()This method returns an array with all the properly ordered members of the queue.
toString()A string depiction of this collection is returned by this method.
take()This function gets and gets rid of the ArrayBlockingQueue’s top element.

Example for @ArrayBlockingQueue in Java

Run
// Java program to demonstrate
// ArrayBlockingQueue(int initialCapacity)
// constructor

import java.util.concurrent.ArrayBlockingQueue;

 class Main {

	public static void main(String[] args)
	{
		// intializing the capacity of ArrayBlockingQueue
		int cap = 10;

		// creating a new object of ArrayBlockingQueue
		// using ArrayBlockingQueue(int initialCapacity) constructor
		ArrayBlockingQueue queue1 = new ArrayBlockingQueue(cap);

		// add numbers
		queue1.add(10);
		queue1.add(20);
		queue1.add(30);
		queue1.add(40);
		queue1.add(50);

		// printing the queue
		System.out.println("ArrayBlockingQueue:" + queue1);
	}
}

Output

ArrayBlockingQueue:[10, 20, 30, 40, 50]

Use of ArrayBlockingQueue in Java

It is regarded as a collection that is thread-safe. As a result, multi-threading programmes typically use it.

Imagine that one thread is adding items to the queue while another is removing items from the queue.

The array blocking queue can force the second thread to wait until the first thread has finished its operations if the first thread is slower than the second thread.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription