0% found this document useful (0 votes)
4 views3 pages

Upper Knowledge of Functional Streams With Parallel Streams in Java

The document discusses the use of functional streams with parallel streams in Java for efficient and thread-safe data processing. It explains how parallel streams divide data for concurrent processing, highlights key functional operations, and warns against side effects when using shared state. Additionally, it mentions the option to use a custom thread pool for better control over parallel execution.

Uploaded by

splusmm24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Upper Knowledge of Functional Streams With Parallel Streams in Java

The document discusses the use of functional streams with parallel streams in Java for efficient and thread-safe data processing. It explains how parallel streams divide data for concurrent processing, highlights key functional operations, and warns against side effects when using shared state. Additionally, it mentions the option to use a custom thread pool for better control over parallel execution.

Uploaded by

splusmm24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Functional streams with parallel streams in Java

- by utk

Using functional streams with parallel streams in Java lets you write concise, efficient,
and thread-safe code to process large datasets in parallel. This takes advantage of multi-
core processors without manual thread handling.

🔹 What Are Parallel Streams?


Parallel streams split the data into multiple chunks, process them in parallel (using the
ForkJoinPool), and then combine the results.

java
CopyEdit
list.parallelStream() // instead of list.stream()

Good for CPU-intensive tasks or large collections


Not ideal for small collections or operations with side effects (like logging or modifying
shared variables)

🔹 Functional-Style + Parallel Stream Example

👇 Example: Filtering, Mapping, Reducing (Parallel)

java
CopyEdit
import java.util.*;
import java.util.stream.*;

public class ParallelStreamExample {


public static void main(String[] args) {
List<Integer> numbers = IntStream.rangeClosed(1, 1_000_000)
.boxed()
.collect(Collectors.toList())
;

long start = System.currentTimeMillis();

int result = numbers.parallelStream()


.filter(n -> n % 2 == 0)
.map(n -> n * 2)
.reduce(0, Integer::sum);

long end = System.currentTimeMillis();

System.out.println("Sum: " + result);


System.out.println("Time taken (ms): " + (end - start));
}
}

🔹 Key Functional Operations in Parallel


Operati
Description Example
on
.filte Keeps elements that match a
stream.filter(x -> x > 10)
r() condition
stream.map(String::toUpper
.map() Transforms elements
Case)
.reduc Aggregates elements to a single stream.reduce(0,
e() result Integer::sum)
.colle Gathers results into a list, set, stream.collect(Collectors.
ct() map, etc. toList())

🔹 Thread-Safety Warning with Parallel Streams


Avoid side-effects (modifying shared state):

Dangerous:
java
CopyEdit
List<Integer> results = new ArrayList<>();
list.parallelStream().forEach(results::add); // May throw
ConcurrentModificationException

Safe:

java
CopyEdit
List<Integer> results = list.parallelStream()
.map(x -> x * 2)
.collect(Collectors.toList());

🔹 Custom Thread Pool (Advanced)


By default, parallelStream() uses the common ForkJoinPool. For better control:

java
CopyEdit
ForkJoinPool customThreadPool = new ForkJoinPool(4);

customThreadPool.submit(() -> {
list.parallelStream()
.forEach(System.out::println);
}).get();

You might also like