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 ExecutableTask and specify that it implements the Callable interface parameterized by the String class:
        public class ExecutableTask implements Callable<String> {
  1. Declare a private String attribute called name. It will store the name of the task. Implement the getName() method to return the value of this attribute:
        private final String name;         public String getName(){           return name;         }
  1. Implement the constructor of the class to initialize the name of the task:
        public ExecutableTask(String name){           this.name=name;         }
  1. Implement the call() method. Put the task to sleep for a random period of time and return a message with the name of the task:
 @Override ...

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