ParalleleStream
Test Simple
public class SimpleParallele {
public static void main(String[] args) {
System.out.println("Normal...");
IntStream range = IntStream.rangeClosed(1, 10);
range.forEach(System.out::println);
System.out.println("Parallel...");
IntStream range2 = IntStream.rangeClosed(1, 10);
range2.parallel().forEach(System.out::println);
}
}
Résultat
Normal...
1
2
3
4
5
6
7
8
9
10
Parallel...
7
6
8
1
2
4
10
3
9
5
Test 2 Test des nombres premiers
public static void main(String[] args) {
long now=System.currentTimeMillis();
long count = Stream.iterate(0, n -> n + 1)
.limit(1_00000)
.filter(Prime::isPrime)
.peek(x -> System.out.format("%s\t", x))
.count();
System.out.println("\nTotal: " + count);
System.err.println("Time :" + (System.currentTimeMillis()-now));
now=System.currentTimeMillis();
count = Stream.iterate(0, n -> n + 1)
.limit(1_00000)
.parallel()
.filter(Prime::isPrime)
.peek(x -> System.out.format("%s\t", x))
.count();
System.out.println("\nTotal: " + count);
System.err.println("Time :" + (System.currentTimeMillis()-now));
}
public static boolean isPrime(int number) {
if (number <= 1) return false;
return !IntStream.rangeClosed(2, number / 2).anyMatch(i -> number % i == 0);
}
Résultat
Total: 9592
Time :1455
Total: 9592
Time :392