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

Perform the following steps to implement the example:

  1. Create a class named Task that implements the Runnable interface:
        public class Task implements Runnable { 
  1. Implement the run() method of the task:
        @Override         public void run() { 
  1. Create a loop with 100 steps:
        for (int i=0; i<100; i++) { 
  1. In each step, put the thread to sleep for 100 milliseconds:
        try {           TimeUnit.MILLISECONDS.sleep(100);         } catch (InterruptedException e) {           e.printStackTrace();         } 
  1. Write a message in the console with the name of the thread and the step number:
              System.out.printf("%s: %d\n",Thread.currentThread()                                                  .getName(),i);                  }           }         } 
  1. Create the main class of the example. Create a class named Main with a main() method:
 public class Main ...

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