Java: parallel streams and CompletableFuture
A parallel stream splits its data across the common fork/join pool and combines the partial results. It suits independent, stateless operations such as filtering and counting, where the answer does not depend on the order in which pieces finish.
Java
import java.util.concurrent.*;
import java.util.stream.*;
class Main {
public static void main(String[] args) throws Exception {
long evens = LongStream.rangeClosed(1, 1_000_000).parallel().filter(n -> n % 2 == 0).count();
System.out.println("evens: " + evens);
CompletableFuture<Integer> price = CompletableFuture.supplyAsync(() -> 40);
CompletableFuture<Integer> tax = CompletableFuture.supplyAsync(() -> 2);
int total = price.thenCombine(tax, Integer::sum).get();
System.out.println("total: " + total);
}
}