How to do it...

Follow these steps to implement the example:

  1. Create a class named Task and specify that it implements the Callable interface parameterized by the String class. Implement the call() method. Write a message to the console and put it to sleep for 100 milliseconds inside an infinite loop:
        public class Task implements Callable<String> {           @Override           public String call() throws Exception {             while (true){               System.out.printf("Task: Test\n");               Thread.sleep(100);             }           }
  1. Implement the main class of the example by creating a class named Main and adding the main() method to it:
        public class Main {           public static void main(String[] args) {
  1. Create a ThreadPoolExecutor object using the newCachedThreadPool() method of the Executors ...

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.