How to do it...

Follow these steps to implement the example:

  1. Create a class named TaskLocalRandom and specify that it implements the Runnable interface:
        public class TaskLocalRandom implements Runnable { 
  1. Implement the run() method. Get the name of the thread that is executing this task and write 10 random integer numbers to the console using the nextInt() method:
        @Override         public void run() {           String name=Thread.currentThread().getName();           for (int i=0; i<10; i++){             System.out.printf("%s: %d\n",name,                              ThreadLocalRandom.current().nextInt(10));           }         } 
  1. Implement the main class of the example by creating a class named Main and add the main() method to it:
        public class Main {           public static void main(String[] args) { 
  1. Create an array ...

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.