Java - Delay Queue

Ömer Kurular
3 min readMay 1, 2021

Delay queues are thread-safe data structures that you can pull elements off after given delay.

1- How to use delay queue?

Unlike any other queues, delay queue accepts objects of classes that implements Delayed interface which also extends Comparable interface which you can see below.

public interface Delayed extends Comparable<Delayed> {
long getDelay(TimeUnit unit);
}
public interface Comparable<T> {
public int compareTo(T o);
}

So, when you create your own object that will put into delayed queue, you will implement these methods. Let’s create our own Delayed class.

public class DelayedItem implements Delayed {
private String name;
private long startingTime;

public DelayedItem(String name, long delay) {
this.name = name;
this.startingTime = System.currentTimeMillis() + delay;
}

public String getName() {
return name;
}

@Override
public long getDelay(TimeUnit unit) {
long delay = startingTime - System.currentTimeMillis();
return unit.toMillis(delay);
}

@Override
public int compareTo(Delayed o) {
return Long.compare(startingTime, ((DelayedItem) o).startingTime);
}
}

In the example above, we take two parameters to initialize a Delayed object. First one is name and this is just for demo purpose and will use later. Second one is the delay that we can get object from queue after. As you notice, we created our startingTime field by using given delay and current time. This field indicated that after this time, you can pull this object off the queue. Then, we implemented getDelay and compareTo methods. getDelay simply returns the delay as its name suggests and compareTo does comparison to sort objects in queue according to their time of being able to be pulled off.

Now it is time to use our delayed item and delay queue.

public static void main(String[] args) {
DelayQueue<DelayedItem> delayedItems = new DelayQueue<>();
DelayedItem delayedItem1 = new DelayedItem("item1", 1000);
DelayedItem delayedItem2 = new DelayedItem("item2", 500);
DelayedItem delayedItem3 = new DelayedItem("item3", 750);
DelayedItem delayedItem4 = new DelayedItem("item3", 1250);

delayedItems.add(delayedItem1);
delayedItems.add(delayedItem2);
delayedItems.add(delayedItem3);
delayedItems.add(delayedItem4);

while (!delayedItems.isEmpty()) {
try {
System.out.println(delayedItems.take().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

Firstly, we create some elements each of which has different delays and then put them into the queue. Then, we pull elements off the queue and print their names into console. Output will be

item2
item3
item1
item3

As you can see, item2 is put to queue as the third but as it has the lowest delay among other elements, it is pulled off first and others lined up according to their delays.

2- When to use delay queue?

Delay queues can be used anywhere according to needs and it will adapt to multi-threaded environments easily as it is thread safe data structure. One example could be that when you have a task where you start a process and wait for acknowledgement with a timeout, you can use delay queue.

In this tutorial, I talked about delay queues and how to use them. I hope you enjoy it.

--

--

Ömer Kurular

I am Ömer and currently working as a full-time software engineer. I will share my knowledge with you I gained through years of professional and self working.