Java - Stream Parallel Streams

Introduction

Streams can be sequential or parallel.

Operations on a sequential stream are processed in serial using one thread.

Operations on a parallel stream are processed in parallel using multiple threads.

To produce a parallel stream from a collection such as a List or a Set, call the parallelStream() method of the Collection interface.

parallel() method on a stream converts a sequential stream into a parallel stream.

sequential() method on a stream converts a parallel stream into a sequential stream.

The following code shows serial processing of the stream because the stream is sequential:

String names = Person.persons()               // The data source
                     .stream()                // Produces a sequential stream
                     .filter(Person::isMale)  // Processed in serial
                     .map(Person::getName)    // Processed in serial
                     .collect(Collectors.joining(", ")); // Processed in serial

The following code shows parallel processing of the stream because the stream is parallel:

String names = Person.persons()           // The data source
                  .parallelStream()       // Produces a parallel stream
                  .filter(Person::isMale) // Processed in parallel
                  .map(Person::getName)   // Processed in parallel
                  .collect(Collectors.joining(", ")); // Processed in parallel

The following code shows processing of the stream in mixed mode.

String names = Person.persons()          // The data source
                .stream()                // Produces a sequential stream
                .filter(Person::isMale)  // Processed in serial
                .parallel()              // Produces a parallel stream
                .map(Person::getName)    // Processed in parallel
                .collect(Collectors.joining(", ")); // Processed in parallel

The following code benchmarks serial and parallel stream.:

Demo

import java.time.LocalTime;
import java.util.stream.IntStream;

public class Main {
  public static void main(String[] args) {
    // Process the stream in serial
    System.out.println(LocalTime.now());
    long count = IntStream.rangeClosed(2, Integer.MAX_VALUE / 10).filter(Main::isPrime).count();
    System.out.println(count);//from   w  ww.  ja  va 2 s. com
    System.out.println(LocalTime.now());
    // Process the stream in parallel
    count = IntStream.rangeClosed(2, Integer.MAX_VALUE / 10).parallel().filter(Main::isPrime).count();
    System.out.println(count);
    System.out.println(LocalTime.now());
  }

  // Checks if the specified number is a prime number
  public static boolean isPrime(long number) {
    // <= 1 is not a prime number
    if (number <= 1) {
      return false;
    }

    // 2 is a prime number
    if (number == 2) {
      return true;
    }

    // Even numbers > 2 are not prime numbers
    if (number % 2 == 0) {
      return false;
    }

    long maxDivisor = (long) Math.sqrt(number);
    for (int counter = 3; counter <= maxDivisor; counter += 2) {
      if (number % counter == 0) {
        return false;
      }
    }

    return true;
  }
}

Result