The Wayback Machine - https://web.archive.org/web/20200420054804/https://www.geeksforgeeks.org/arrayblockingqueue-class-in-java/amp/

ArrayBlockingQueue Class in Java

ArrayBlockingQueue class is a bounded blocking queue backed by an array. By bounded it means that the size of the Queue is fixed. Once created, the capacity cannot be changed. Attempts to put an element into a full queue will result in the operation blocking. Similarly attempts to take an element from an empty queue will also be blocked. Boundness of the ArrayBlockingQueue can be achieved initially by passing capacity as the parameter in the constructor of ArrayBlockingQueue. This queue orders elements FIFO (first-in-first-out). It means that the head of this queue is the oldest element of the elements present in this queue. The tail of this queue is the newest element of the elements of this queue. The newly inserted elements are always inserted at the tail of the queue, and the queue retrieval operations obtain elements at the head of the queue.

This class and its iterator implement all of the optional methods of the Collection and Iterator interfaces. This class is a member of the Java Collections Framework.

Class Heirarchy:



java.lang.Object 
    ↳ java.util.AbstractCollection 
        ↳ java.util.AbstractQueue 
            ↳ java.util.concurrent.ArrayBlockingQueue

Type Parameters: The type parameter of ArrayBlockingQueue E is the type of elements held in this collection

Implemented Interfaces: Following are the interfaces implemented by the ArrayBlockingQueue Class

Syntax:

public class ArrayBlockingQueue
    extends AbstractQueue
        implements BlockingQueue, Serializable

Constructor Summary:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// ArrayBlockingQueue(int initialCapacity) constructor
  
import java.util.concurrent.ArrayBlockingQueue;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // define capacity of ArrayBlockingQueue
        int capacity = 15;
  
        // create object of ArrayBlockingQueue
        // using ArrayBlockingQueue(int initialCapacity) constructor
        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);
    }
}
chevron_right

Output:
ArrayBlockingQueue:[1, 2, 3]

Method Summary:

Example 1: Adding elements in ArrayBlockingQueue

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.concurrent.ArrayBlockingQueue;
  
public class GFG {
  
    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);
    }
}
chevron_right

Output:
ArrayBlockingQueue:[1, 2, 3]

Example 2: Clearing the ArrayBlockingQueue

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.concurrent.ArrayBlockingQueue;
  
public class GFG {
  
    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);
    }
}
chevron_right

Output:
ArrayBlockingQueue:[1, 2, 3]
ArrayBlockingQueue:[]




Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : ManasChhabra2

Article Tags :