ArrayBlockingQueue Class in Java


A bounded blocking queue that is backed by an array is known as a ArrayBlockingQueue Class in Java. The size of the queue is fixed in the class and it uses FIFO for ordering elements. The ArrayBlockingQueue Class is a member of the Java Collections Framework.

A program that demonstrates this is given as follows −

Example

 Live Demo

import java.util.concurrent.ArrayBlockingQueue;
public class Demo {
   public static void main(String[] args) {
      int n = 10;
      ArrayBlockingQueue<Integer> abQueue = new ArrayBlockingQueue<Integer>(n);
      abQueue.add(7);
      abQueue.add(2);
      abQueue.add(6);
      abQueue.add(3);
      abQueue.add(1);
      System.out.println("The elements in ArrayBlockingQueue are: " + abQueue);
   }
}

The output of the above program is as follows −

Output

The elements in ArrayBlockingQueue are: [7, 2, 6, 3, 1]

Now let us understand the above program.

The ArrayBlockingQueue is created with the capacity 10. Then elements are added to it and finally, it is displayed. A code snippet that demonstrates this is given as follows −

int n = 10;
ArrayBlockingQueue<Integer> abQueue = new ArrayBlockingQueue<Integer>(n);
abQueue.add(7);
abQueue.add(2);
abQueue.add(6);
abQueue.add(3);
abQueue.add(1);
System.out.println("The elements in ArrayBlockingQueue are: " + abQueue);

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

76 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements