Multithreading: ArrayBlockingQueue is bounded, unlike LinkedBlockingQueue

An ArrayBlockingQueue is created with a fixed capacity and offer() returns false once it is full, whereas a no-argument LinkedBlockingQueue keeps accepting elements until memory runs out. Choosing the bounded variant is how you apply backpressure to a fast producer.

Code
ArrayBlockingQueue<Integer> bounded = new ArrayBlockingQueue<>(2);
bounded.offer(1);
bounded.offer(2);
boolean rejected = !bounded.offer(3);
LinkedBlockingQueue<Integer> unbounded = new LinkedBlockingQueue<>();
for (int i = 0; i < 5; i++) unbounded.offer(i);
System.out.println("Bounded queue rejected a third item: " + rejected);
System.out.println("Bounded queue contents: " + bounded);
System.out.println("Unbounded queue size: " + unbounded.size());
Output
Bounded queue rejected a third item: true
Bounded queue contents: [1, 2]
Unbounded queue size: 5
Advertisement
More in JAVA

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-09-27

© Java Coding Hub · About · Contact · Privacy · Terms