What is a Java queue?

Java queues are fundamental data structures for a wide variety of applications and use cases, helping you keep track of the items in an ordered sequence. But what is a Java queue exactly, and how do Java queues work?

What are Java queues?

A queue is an abstract data type that keeps track of objects in a sequence based on a FIFO (first in, first out) order. New objects are always added to the end of the queue (also called the tail or rear), and always removed from the front of the queue (also called the head). You can think of queues as modeling the behavior of waiting in line at a bank or in an office: people who arrive first also receive help first.

Java queues are implementations of the queue abstract data type in the Java programming language. The queue data structure is implemented in the java.util.Queue interface. Java queues have a variety of possible use cases. For example, during times of high demand for a web application, you can store the requests you receive in a queue, and service the requests that came earlier first.

How do Java queues work?

The java.util.Queue interface defines the methods that must be part of any class that implements the interface. Below are some of the most important methods of Java queues:

  • add(): Adds an element to the tail of the queue; throws an exception if the queue is full.
  • clear(): Removes all of the elements from the queue.
  • isEmpty(): Returns a Boolean value based on whether the queue is empty.
  • offer(): Adds an element to the tail of the queue only if the queue is not already full.
  • peek(): Looks at the element at the head of the queue without removing it.
  • poll(): Removes and returns the element at the head of the queue; returns null if the queue is empty.
  • remove(): Removes and returns the element at the head of the queue; throws an exception if the queue is empty.
  • size(): Returns the number of elements in the queue.

Because java.util.Queue is only an interface, it cannot be instantiated directly in Java. Instead, users need to instantiate one of the classes that implement the Queue interface, such as AbstractQueue, LinkedBlockingQueue, and LinkedTransferQueue. The differences between these classes is as follows:

  • AbstractQueue: The AbstractQueue class is the most bare-bones implementation of Java queues, and does not include methods such as offer().
  • LinkedBlockingQueue: By default, Java queues return null or throw an exception during unexpected behavior, such as removing from an empty queue or inserting an element in a full queue. When a thread performs one of these actions to a LinkedBlockingQueue, the queue instead blocks the thread, forcing it to wait until the operation can continue (i.e. an element arrives in the queue, or the queue is no longer full).
  • LinkedTransferQueue: Transfer queues extend the concept of the LinkedBlockingQueue and adapts it for the producer-consumer pattern. As the name suggests, both LinkedBlockingQueues and LinkedTransferQueues are implemented in Java with the linked list data structure.

Java queues in Redis

Redis is an open-source, in-memory data structure store used to implement NoSQL key-value databases, caches, and message brokers. When Redis is used as a message broker, it needs to send and receive messages in the correct order—which is the perfect use case for data structures such as queues.

However, Redis only includes a few built-in data structures: lists, sets, strings, hashes, and sorted sets. The good news is that developers can build their own queue data structure using Redis lists. Redis even includes list functions such as RPUSH, LPUSH, RPOP, and LPOP to help with the implementation.

Still, this isn’t an ideal solution, especially for developers using languages such as Java who are used to a rich standard library. To fix this issue and lower the Redis learning curve, many Java developers choose to install a third-party Redis Java client such as Redisson.

Redisson includes many familiar Java objects, collections, and constructs for Redis, including Java queues. The queue data structure is implemented in Redisson using the RQueue interface, which extends the java.util.Queue interface. Below is an example of how to use Java queues in Redis with Redisson:

RQueue<SomeObject> queue = redisson.getQueue("anyQueue");
queue.add(new SomeObject());
SomeObject obj = queue.peek();
SomeObject someObj = queue.poll();

What’s more, Redisson also includes implementations of special kinds of Java queues, such as blocking queues, bounded blocking queues, and delayed queues.

Similar terms