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

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.