O'Reilly logo

Java 9 Concurrency Cookbook - Second Edition by Javier Fernández González

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required