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 Main with a main() method. Declare two private variables, namely an AtomicInteger variable called counter and a Random object called random:
        public class Main {           public static void main(String[] args) {              AtomicLong counter = new AtomicLong(0);             Random random=new Random();
  1. Create a stream of 1,000 random double numbers. The stream created is a sequential stream. You have to make it parallel using the parallel() method, and use the peek() method to increment the value of the counter variable and write a message in the console. Post this, use the count() method to count the number of elements in the array and store that number in an integer variable. Write ...

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