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

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