How to do it...

Follow these steps to implement the example:

  1. Create a class named Task and specify that it implements the Runnable interface:
        public class Task implements Runnable {
  1. Declare a private String attribute called name that will store the name of the task:
        private final String name;
  1. Implement the constructor of the class that initializes the attribute:
        public Task(String name) {           this.name=name;         }
  1. Implement the run() method. Write a message to the console with the actual date to verify that the task is executed within the specified period:
        @Override         public void run() {           System.out.printf("%s: Executed at: %s\n",name,new Date());         }
  1. Implement the main class of the example by creating a class named Main and adding ...

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.