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
importjava.util.concurrent.ArrayBlockingQueue;publicclassGeeks{publicstaticvoidmain(String[]args)throwsInterruptedException{// Create an ArrayBlockingQueue with a capacity of 3ArrayBlockingQueue<Integer>q=newArrayBlockingQueue<>(3);// Adding elements to the queueq.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 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
importjava.util.concurrent.ArrayBlockingQueue;publicclassGeeks{publicstaticvoidmain(String[]args){// define capacity of ArrayBlockingQueueintc=15;// create object of ArrayBlockingQueue using ArrayBlockingQueue(int initialCapacity) constructorArrayBlockingQueue<Integer>a=newArrayBlockingQueue<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
importjava.util.concurrent.ArrayBlockingQueue;publicclassGeeks{publicstaticvoidmain(String[]args){// define capacity of ArrayBlockingQueueintc=15;// create object of ArrayBlockingQueueArrayBlockingQueue<Integer>a=newArrayBlockingQueue<Integer>(c);// add numbersa.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
importjava.util.concurrent.ArrayBlockingQueue;publicclassGeeks{publicstaticvoidmain(String[]args){// define capacity of ArrayBlockingQueueintc=15;// create object of ArrayBlockingQueueArrayBlockingQueue<Integer>a=newArrayBlockingQueue<Integer>(c);// add numbersa.add(1);a.add(2);a.add(3);System.out.println("ArrayBlockingQueue: "+a);booleanb=a.remove(2);System.out.println("Element 2 removed ?: "+b);System.out.println("Updated ArrayBlockingQueue: "+a);}}
3. Accessing Elements: We can use the peek() method to retrieve the head of the queue without removing it.
Java
importjava.util.concurrent.ArrayBlockingQueue;publicclassGeeks{publicstaticvoidmain(String[]args){// Define capacity of ArrayBlockingQueueintc=5;// Create object of ArrayBlockingQueueArrayBlockingQueue<Integer>q=newArrayBlockingQueue<Integer>(c);// Add element to ArrayBlockingQueueq.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
importjava.util.*;importjava.util.concurrent.ArrayBlockingQueue;publicclassGeeks{publicstaticvoidmain(String[]args){// Define capacity of ArrayBlockingQueueintc=5;ArrayBlockingQueue<String>q=newArrayBlockingQueue<String>(c);q.offer("Java");q.offer("C++");q.offer("Python");q.offer("Js");System.out.println("ArrayBlockingQueue is: "+q);Iteratori=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
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.
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.
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.
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.