How to do it...

Follow these steps to implement the example:

  1. First, we'll implement some auxiliary classes we will use in the example. First, implement the Person class that stores the basic attributes of a person, and the PersonGenerator class that generates a List of random Person objects. Please, check the recipe Apply an action to all the elements of a stream to see the source code of both the classes.
  2. Then, we'll implement the Main class with the main() method. First, create a List of random Person objects using the PersonGenerator class. Use the forEach() method to print the generated elements:
        public class Main {           public static void main(String[] args) {             List<Person> persons=PersonGenerator                                        .generatePersonList(10);  persons.parallelStream().forEach(p-> ...

Get Java 9 Concurrency Cookbook - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.