Java ArrayBlockingQueue
Java ArrayBlockingQueue
Here,
Methods of ArrayBlockingQueue
• The ArrayBlockingQueue class provides the implementation of all the
methods in the BlockingQueue interface.
• These methods are used to insert, access and delete elements from array
blocking queues.
Insert Elements
• add() - Inserts the specified element to the array blocking queue. It throws
an exception if the queue is full.
• offer() - Inserts the specified element to the array blocking queue. It
returns false if the queue is full.
For example,
import java.util.concurrent.ArrayBlockingQueue;
class Main {
public static void main(String[] args) {
ArrayBlockingQueue<String> animals = new ArrayBlockingQueue<>(5);
// Using add()
animals.add("Dog");
animals.add("Cat");
// Using offer()
animals.offer("Horse");
System.out.println("ArrayBlockingQueue: " + animals);
}
}
Output
ArrayBlockingQueue: [Dog, Cat, Horse]
Access Elements
• peek() - Returns an element from the front of the array blocking queue. It
returns null if the queue is empty.
• iterator() - Returns an iterator object to sequentially access elements from
the array blocking queue. It throws an exception if the queue is empty. We
must import the java.util.Iterator package to use it.
For example,
import java.util.concurrent.ArrayBlockingQueue;
import java.util.Iterator;
class Main {
public static void main(String[] args) {
ArrayBlockingQueue<String> animals = new ArrayBlockingQueue<>(5);
// Add elements
animals.add("Dog");
animals.add("Cat");
animals.add("Horse");
System.out.println("ArrayBlockingQueue: " + animals);
// Using peek()
String element = animals.peek();
System.out.println("Accessed Element: " + element);
// Using iterator()
Iterator<String> iterate = animals.iterator();
System.out.print("ArrayBlockingQueue Elements: ");
while(iterate.hasNext()) {
System.out.print(iterate.next());
System.out.print(", ");
}
}
}
Output
import java.util.concurrent.ArrayBlockingQueue;
class Main {
public static void main(String[] args) {
ArrayBlockingQueue<String> animals = new ArrayBlockingQueue<>(5);
animals.add("Dog");
animals.add("Cat");
animals.add("Horse");
System.out.println("ArrayBlockingQueue: " + animals);
// Using remove()
String element1 = animals.remove();
System.out.println("Removed Element:");
System.out.println("Using remove(): " + element1);
// Using poll()
String element2 = animals.poll();
System.out.println("Using poll(): " + element2);
// Using clear()
animals.clear();
System.out.println("Updated ArrayBlockingQueue: " + animals);
}
}
Output
For example,
import java.util.concurrent.ArrayBlockingQueue;
class Main {
public static void main(String[] args) {
ArrayBlockingQueue<String> animals = new ArrayBlockingQueue<>(5);
try {
// Add elements to animals
animals.put("Dog");
animals.put("Cat");
System.out.println("ArrayBlockingQueue: " + animals);
}
catch(Exception e) {
System.out.println(e);
}
}
}
Output
take() Method
• To return and remove an element from the front of the array blocking
queue, we can use the take() method.
• If the array blocking queue is empty, it waits until there are elements in
the array blocking queue to be deleted.
For example,
import java.util.concurrent.ArrayBlockingQueue;
class Main {
public static void main(String[] args) {
ArrayBlockingQueue<String> animals = new ArrayBlockingQueue<>(5);
try {
//Add elements to animals
animals.put("Dog");
animals.put("Cat");
System.out.println("ArrayBlockingQueue: " + animals);
// Remove an element
String element = animals.take();
System.out.println("Removed Element: " + element);
}
catch(Exception e) {
System.out.println(e);
}
}
}
Output
Methods Descriptions