GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — ArrayBlockingQueue Class in Java - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

ArrayBlockingQueue Class in Java

Last Updated : 13 Apr, 2026

In Java, the ArrayBlockingQueue class is part of the java.util.concurrent package and implements the BlockingQueue interface. It is a thread-safe, bounded queue that helps manage producer-consumer scenarios by blocking threads when the queue is full or empty.

  • The queue has a fixed size, specified during creation.
  • Methods like take() and put() block the calling thread when the queue is full or empty
  • It is safe for use in multi-threaded environments without needing additional synchronization.

Example: Use ArrayBlockingQueue with a fixed capacity, blocking if the queue is full.

Java
import java.util.concurrent.ArrayBlockingQueue;

public class Geeks {
    public static void main(String[] args)
        throws InterruptedException
    {

        // Create an ArrayBlockingQueue with a capacity of 3
        ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<>(3);

        // Adding elements to the queue
        q.put(1);
        q.put(2);
        q.put(3);

        System.out.println("Queue after adding elements: " + q);
    }
}

Output
Queue after adding elements: [1, 2, 3]

ArrayBlockingQueue Hierarchy

ArrayBlockingQueue is a part of the Java Collections Framework and extends AbstractQueue while implementing the BlockingQueue interface.

ArrayBlockingQueue-Class-in-Java
ArrayBlockingQueue Hierarchy

Declaration of ArrayBlockingQueue

In Java, the declaration of ArrayBlockingQueue can be done as:

ArrayBlockingQueue<Type> queue = new ArrayBlockingQueue<>(capacity)

Note: Here Type is the type of element the queue will hold (e.g Integer, String) and capacity is the maximum number of elements the queue can hold.

Constructors

Constructor

Description

ArrayBlockingQueue(int capacity)

Creates an ArrayBlockingQueue with the given (fixed) capacity and default access policy. 

ArrayBlockingQueue(int capacity, boolean fair)

reates a queue with the specified capacity and a fair or non-fair locking policy based on the fair argument.

ArrayBlockingQueue(int capacity, boolean fair, Collection c)

Creates a queue with the specified capacity, locking policy, and initializes it with elements from the given collection

Example: Creation of an ArrayBlockingQueue with a specified capacity and adding elements to it.

Java
import java.util.concurrent.ArrayBlockingQueue;

public class Geeks {

    public static void main(String[] args){
       // define capacity of ArrayBlockingQueue
        int c = 15;

        // create object of ArrayBlockingQueue using ArrayBlockingQueue(int initialCapacity) constructor
        ArrayBlockingQueue<Integer> a = new ArrayBlockingQueue<Integer>(c);

        a.add(1);
        a.add(2);
        a.add(3);

        System.out.println("ArrayBlockingQueue:" + a);
    }
}

Output
ArrayBlockingQueue:[1, 2, 3]

Performing Various Operations on ArrayBlockingQueue

1. Adding Elements: We use add() method to insert elements into an ArrayBlockingQueue.

Java
import java.util.concurrent.ArrayBlockingQueue;

public class Geeks {

    public static void main(String[] args)
    {
        // define capacity of ArrayBlockingQueue
        int c = 15;

        // create object of ArrayBlockingQueue
        ArrayBlockingQueue<Integer> a= new ArrayBlockingQueue<Integer>(c);

        // add numbers
        a.add(100);
        a.add(200);
        a.add(300);

        System.out.println("ArrayBlockingQueue : " + a);
    }
}

Output
ArrayBlockingQueue : [100, 200, 300]

2. Removing Elements: We can use remove() method to remove elements from the ArrayBlockingQueue.

Java
import java.util.concurrent.ArrayBlockingQueue;
public class Geeks{

    public static void main(String[] args)
    {
        // define capacity of ArrayBlockingQueue
        int c = 15;

        // create object of ArrayBlockingQueue
        ArrayBlockingQueue<Integer> a = new ArrayBlockingQueue<Integer>(c);

        // add numbers
        a.add(1);
        a.add(2);
        a.add(3);

        System.out.println("ArrayBlockingQueue: " + a);

        boolean b = a.remove(2);

        System.out.println("Element 2 removed ?: " + b);
        System.out.println("Updated ArrayBlockingQueue: " + a);
        
    }
}

Output
ArrayBlockingQueue: [1, 2, 3]
Element 2 removed ?: true
Updated ArrayBlockingQueue: [1, 3]

3. Accessing Elements: We can use the peek() method to retrieve the head of the queue without removing it.

Java
import java.util.concurrent.ArrayBlockingQueue;
public class Geeks{

    public static void main(String[] args){
        // Define capacity of ArrayBlockingQueue
        int c = 5;

        // Create object of ArrayBlockingQueue
        ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(c);

        // Add element to ArrayBlockingQueue
        q.add(23);
        q.add(32);
        q.add(45);
        q.add(12);

        System.out.println("ArrayBlockingQueue is: " + q);
        System.out.println("Head of queue is: " + q.peek());
    }
}

Output
ArrayBlockingQueue is: [23, 32, 45, 12]
Head of queue is: 23

4. Iterating Elements: We use iterator() method to iterate over the elements of an ArrayBlockingQueue in the order from head to tail.

Java
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;

public class Geeks {

    public static void main(String[] args)
    {
        // Define capacity of ArrayBlockingQueue
        int c = 5;

        ArrayBlockingQueue<String> q = new ArrayBlockingQueue<String>(c);

        q.offer("Java");
        q.offer("C++");
        q.offer("Python");
        q.offer("Js");

        System.out.println("ArrayBlockingQueue is: " + q);

        Iterator i = q.iterator();

        System.out.println("The iterator values:");
        while (i.hasNext()) {
            System.out.print(i.next() + " ");
        }
    }
}

Output
ArrayBlockingQueue is: [Java, C++, Python, Js]
The iterator values:
Java C++ Python Js 

Common Methods

Methods

Description

add​(E e)Inserts the specified element at the tail of this queue if it is possible to do so immediately without exceeding the queue's capacity, returning true upon success and throwing an IllegalStateException if this queue is full.
clear()Atomically removes all of the elements from this queue.
contains​(Object o)Returns true if this queue contains the specified element.
drainTo​(Collection<? super E> c)Removes all available elements from this queue and adds them to the given collection.
drainTo​(Collection<? super E> c, int maxElements)Removes at most the given number of available elements from this queue and adds them to the given collection.
forEach​(Consumer<? super E> action)Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.
iterator()Returns an iterator over the elements in this queue in the proper sequence.
offer​(E e)Inserts the specified element at the tail of this queue if it is possible to do so immediately without exceeding the queue's capacity, returning true upon success and false if this queue is full.
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.
put​(E e)Inserts the specified element at the tail of this queue, waiting for space to become available if the queue is full.
remainingCapacity()Returns the number of additional elements that this queue can ideally (in the absence of memory or resource constraints) accept without blocking.
remove​(Object o)Removes a single instance of the specified element from this queue, if it is present.
removeAll​(Collection<?> c)Removes all of this collection's elements that are also contained in the specified collection (optional operation).
removeIf​(Predicate<? super E> filter)Removes all of the elements of this collection that satisfy the given predicate.
retainAll​(Collection<?> c)Retains only the elements in this collection that are contained in the specified collection (optional operation).
size()Returns the number of elements in this queue.
spliterator()Returns a Spliterator over the elements in this queue.
toArray()Returns an array containing all of the elements in this queue, in proper sequence.
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.

Methods Declared in Class java.util.AbstractQueue

Methods

Description

addAll​(Collection<? extends E> c)Adds all of the elements in the specified collection to this queue.
element()Retrieves, but does not remove, the head of this queue.
remove()Retrieves and removes the head of this queue.

Methods Declared in Class java.util.AbstractCollection

Methods

Description

containsAll​(Collection<?> c)Returns true if this collection contains all of the elements in the specified collection.
isEmpty()Returns true if this collection contains no elements.
toString()Returns a string representation of this collection.

Methods Declared in Interface java.util.concurrent.BlockingQueue

Methods

Description

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.
take()Retrieves and removes the head of this queue, waiting if necessary until an element becomes available.

Methods Declared in Interface java.util.Collection

Methods

Description

addAll​(Collection<? extends E> c)Adds all of the elements in the specified collection to this collection (optional operation).
containsAll​(Collection<?> c)Returns true if this collection contains all of the elements in the specified collection.
equals​(Object o)Compares the specified object with this collection for equality.
hashCode()Returns the hash code value for this collection.
isEmpty()Returns true if this collection contains no elements.
parallelStream()Returns a possibly parallel Stream with this collection as its source.
stream()Returns a sequential Stream with this collection as its source.
toArray​(IntFunction<T[]> generator)Returns an array containing all of the elements in this collection, using the provided generator function to allocate the returned array.

Methods Declared in Interface java.util.Queue

Methods

Description

element()Retrieves, but does not remove, the head of this queue.
peek()Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
poll()Retrieves and removes the head of this queue, or returns null if this queue is empty.
remove()Retrieves and removes the head of this queue.


Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.