How it works...

In this recipe, we implemented the Event class. This class has a unique attribute, the activation date of the events, and it implements the Delayed interface. You can store Event objects in the DelayQueue class.

The getDelay() method returns the number of nanoseconds between the activation date and the actual date. Both dates are objects of the Date class. You used the getTime() method that returns a date converted into milliseconds. Then, you converted this value into TimeUnit, which was received as a parameter. The DelayQueue class works in nanoseconds, but at this point, it's transparent to you.

The compareTo() method returns a value less than zero if the delay of the object executing the method is smaller than the delay ...

Get Java 9 Concurrency Cookbook - Second Edition 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.