Let’s see an example of a DelayQueue. A DelayQueue is one of the implementation classes for the BlockingQueue interface. It lets you implement a queue whose elements must stay in a queue for a certain amount of time (known as a delay). How does the DelayQueue know about the amount of time an element has to be kept in the queue? It makes use of an interface called Delayed to know the time an element must stay in the queue. The interface is in the java.util.concurrent package. Its declaration is as follows:

public interface Delayed extends Comparable<Delayed> {        long getDelay(TimeUnit timeUnit);}

It extends the Comparable interface whose compareTo() method accepts a Delayed object. The DelayQueue calls the getDelay() method of each element ...

Get Beginning Java 8 Language Features: Lambda Expressions, Inner Classes, Th reads, I/O, Collections,and Streams now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.