Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

Java BlockingQueue interface represents a thread-safe queue. It blocks a thread trying to put elements in the queue if the queue is full. It also blocks a thread trying to take elements from the queue if the queue is empty.

There are various implementations of BlockingQueue like ArrayBlockingQueue, LinkedBlockingQueue, SynchronousQueue, PriorityBlockingQueue.

In this tutorial, we’ll look at the differences between ArrayBlockingQueue and LinkedBlockingQueue.

2. ArrayBlockingQueue

An ArrayBlockingqueue is a bounded queue. It internally uses an array. We can mention the size of the array when creating its instance.

The below code snippet shows how we can create an object of ArrayBlockingQueue. We have mentioned the size of the internal array as 10:

int INIT_CAPACITY = 10;

BlockingQueue<String> arrayBlockingQueue = new ArrayBlockingQueue<>(INIT_CAPACITY, true);

If we insert elements in the queue beyond the defined capacity and the queue is full, then the add operation throws IllegalStateException. Also, if we set the initial size to less than 1, we’ll get IllegalArgumentException.

Here the second parameter represents the fairness policy. We can optionally set the fairness policy to preserve the order of blocked producer and consumer threads. It allows queue access for blocked threads in FIFO order. Thus, the thread that went into the waiting state first will first get the chance to access the queue. This helps to avoid thread starvation.

3. LinkedBlockingQueue

A LinkedBlockingQueue is an optionally bounded implementation of BlockingQueue. It’s backed by linked nodes.

We can also specify the capacity while creating its instance. If not specified, then Integer.MAX_VALUE is set as capacity.

Linked nodes are dynamically created when elements are inserted.

Let’s see how we can create a LinkedBlockingQueue:

BlockingQueue<String> linkedBlockingQueue = new LinkedBlockingQueue<>();

4. ArrayBlockingQueue vs. LinkedBlockingQueue

Even though both ArrayBlockingQueue and LinkedBlockingQueue are implementations of BlockingQueue and store elements in FIFO order, there are certain differences between them. Now we’ll look at those differences:

Feature ArrayBlockingQueue LinkedBlockingQueue
Implementation It’s backed by an array It uses linked nodes
Queue Size It’s a bounded queue. Therefore, it’s mandatory to specify the initial capacity while creating it. It’s not mandatory to mention size.
Fairness Policy We can set a fairness policy in it There’s no option to set a fairness policy in this
Number of Locks It uses one ReentrantLock. The same lock is used for both put and take operations. It uses separate ReentrantLock for read and write operations. This prevents contention between producer and consumer threads.
Memory space Since it’s mandatory to mention initial capacity in it, we can end up allocating more space than required. It usually doesn’t pre-allocate nodes. Therefore, its memory footprint matches its size

5. Conclusion

In this article, we learned about the differences between ArrayBlockingQueue and LinkedBlockingQueue. The ArrayBlockingQueue is backed up by an array, and the LinkedBlockingQueue by linked nodes. We also touched upon the additional feature of the fairness policy that exists in ArrayBlockingQueue and the locking mechanism and memory footprint of both queues.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW