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. Implement the run() method. Create a loop with 1,000,000 steps, and in each step, do some mathematical operations with an integer variable:
        @Override         public void run() {           int r;           for (int i=0; i<1000000; i++) {             r=0;             r++;             r++;             r*=r;           }         }
  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 1,000 threads to execute 1,000 task objects and wait for their finalization, controlling the total execution time:
 Thread threads[]=new ...

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.