How to do it...

Follow these steps to implement the example:

  1. Create a class named ReportGenerator and specify that it implements the Callable interface parameterized by the String class:
        public class ReportGenerator implements Callable<String> {
  1. Declare two private String attributes named sender and title. These attributes will represent the data of the report:
        private final String sender;         private final String title;
  1. Implement the constructor of the class that initializes the two attributes:
        public ReportGenerator(String sender, String title){           this.sender=sender;           this.title=title;         }
  1. Implement the call() method. First, put the thread to sleep for a random period of time:
        @Override  public String call() throws Exception { ...

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.