Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Java ArrayBlockingQueue: Advanced Guide to Blocking Queues and Thread‑Safe Operations

Java ArrayBlockingQueue

Explore the Java ArrayBlockingQueue – learn how to create, manipulate, and thread‑safely use this fixed‑size blocking queue with clear examples.

The ArrayBlockingQueue class in the Java Collections Framework provides a bounded, thread‑safe queue backed by an array. It implements the BlockingQueue interface, allowing producers and consumers to coordinate via blocking operations.

Java ArrayBlockingQueue: Advanced Guide to Blocking Queues and Thread‑Safe Operations


Creating an ArrayBlockingQueue

Import java.util.concurrent.ArrayBlockingQueue and instantiate with a fixed capacity:

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

Where Type is the element type and capacity is the maximum size.

Note: Capacity is mandatory; it determines the queue’s bounded size.


Core Methods of ArrayBlockingQueue

The class implements all methods from BlockingQueue for inserting, accessing, and removing elements. Below are the most frequently used operations.

Insert Elements

ArrayBlockingQueue<String> animals = new ArrayBlockingQueue<>(5);
animals.add("Dog");
animals.offer("Cat");
animals.offer("Horse");
System.out.println("ArrayBlockingQueue: " + animals);

Output: ArrayBlockingQueue: [Dog, Cat, Horse]

Access Elements

String head = animals.peek();
System.out.println("Head element: " + head);
Iterator<String> it = animals.iterator();
while (it.hasNext()) {
    System.out.print(it.next() + ", ");
}

Output: Head element: Dog followed by the list of elements.

Remove Elements

String removed1 = animals.remove();
String removed2 = animals.poll();
animals.clear();
System.out.println("Updated queue: " + animals);

Output: Updated queue: []


Blocking Operations: put() and take()

These methods are essential for producer‑consumer patterns. They block until the operation can be performed, ensuring thread coordination.

put()

Inserts an element, waiting if the queue is full. It may throw InterruptedException.

try {
    animals.put("Dog");
    animals.put("Cat");
} catch (InterruptedException e) {
    Thread.currentThread().interrupt();
}

take()

Removes and returns the head, waiting if the queue is empty. It may throw InterruptedException.

try {
    String element = animals.take();
    System.out.println("Removed: " + element);
} catch (InterruptedException e) {
    Thread.currentThread().interrupt();
}

Additional Convenience Methods

MethodDescription
contains(Object o)Returns true if the queue contains the specified element.
size()Returns the current number of elements.
toArray()Converts the queue to an array.
toString()String representation of the queue.

Why Use ArrayBlockingQueue?

Because it offers:

When one thread produces slower than the consumer, the consumer blocks automatically, avoiding busy‑waiting and ensuring efficient CPU usage.


Java

  1. Master Java Operators: Types, Syntax, & Practical Examples
  2. Java Comments: Types, Usage, and Best Practices
  3. Mastering Java's super Keyword: Advanced Usage & Practical Examples
  4. Mastering Java Interfaces: Concepts, Implementation, and Best Practices
  5. Mastering Java Try‑with‑Resources: Automatic Resource Management Explained
  6. Java Annotations Explained: Types, Placement, and Practical Examples
  7. Mastering Java’s Queue Interface: Methods, Implementations, and Practical Use
  8. Java PriorityQueue: Fundamentals, Operations, and Custom Sorting
  9. Java BlockingQueue: Thread‑Safe Queues with Blocking Operations
  10. Mastering Java's LinkedBlockingQueue: Comprehensive Guide & Examples