forked from Java-Techie-jt/java8
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParallelStreamExample.java
More file actions
56 lines (38 loc) · 1.93 KB
/
Copy pathParallelStreamExample.java
File metadata and controls
56 lines (38 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.javatechie.parralel_stream;
import com.javatechie.map_reduce.Employee;
import com.javatechie.map_reduce.EmployeeDatabase;
import java.util.List;
import java.util.stream.IntStream;
public class ParallelStreamExample {
public static void main(String[] args) {
long start=0;
long end=0;
start=System.currentTimeMillis();
IntStream.range(1,100).forEach(System.out::println);
end=System.currentTimeMillis();
System.out.println("Plain stream took time : "+(end-start));
System.out.println("============================================");
start=System.currentTimeMillis();
IntStream.range(1,100).parallel().forEach(System.out::println);
end=System.currentTimeMillis();
System.out.println("Parallel stream took time : "+(end-start));
IntStream.range(1,10).forEach(x->{
System.out.println("Thread : "+Thread.currentThread().getName()+" : "+x);
});
IntStream.range(1,10).parallel().forEach(x->{
System.out.println("Thread : "+Thread.currentThread().getName()+" : "+x);
});
List<Employee> employees = EmployeeDatabase.getEmployees();
//normal
start=System.currentTimeMillis();
double salaryWithStream = employees.stream()
.map(Employee::getSalary).mapToDouble(i -> i).average().getAsDouble();
end=System.currentTimeMillis();
System.out.println("Normal stream execution time : "+(end-start)+" : Avg salary : "+salaryWithStream);
start=System.currentTimeMillis();
double salaryWithParallelStream = employees.parallelStream()
.map(Employee::getSalary).mapToDouble(i -> i).average().getAsDouble();
end=System.currentTimeMillis();
System.out.println("Parallel stream execution time : "+(end-start)+" : Avg salary : "+salaryWithParallelStream);
}
}