How to do it...

Follow these steps to implement the example:

  1. Create a class named Event and specify that it implements the Delayed interface:
        public class Event implements Delayed {
  1. Declare a private Date attribute named startDate:
        private final Date startDate;
  1. Implement the constructor of the class to initialize its attribute:
        public Event (Date startDate) {           this.startDate=startDate;         }
  1. Implement the compareTo() method. It receives a Delayed object as its parameter. Return the difference between the delay of the current object and the one passed as a parameter:
        @Override         public int compareTo(Delayed o) {           long result=this.getDelay(TimeUnit.NANOSECONDS)-o.getDelay                                                    (TimeUnit.NANOSECONDS);           if (result<0) {             return -1;  } else ...

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.