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

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