-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParallelProcessing.java
51 lines (40 loc) · 1.97 KB
/
ParallelProcessing.java
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
package java8features;
import java.io.IOException;
import java.util.List;
import java.util.OptionalDouble;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class ParallelProcessing {
public static void main(String[] args) throws IOException {
long start = 0;
long end = 0;
// start = System.currentTimeMillis();
// IntStream.range(1,100).forEach(System.out::println);
// end = System.currentTimeMillis();
// System.out.println("Total time : "+(end-start));
//
// start = System.currentTimeMillis();
// IntStream.range(1,100).parallel().forEach(System.out::println);
// end = System.currentTimeMillis();
// System.out.println("Total time with parallel stream: "+(end-start));
IntStream.range(1, 100).forEach(x->{
System.out.println("Current Thread Name : "+Thread.currentThread().getName());
});
IntStream.range(1, 100).parallel().forEach(x->{
System.out.println("Current Thread Name parallel: "+Thread.currentThread().getName());
});
List<Employee> employees = EmployeeDatabase.getEmployees();
start = System.currentTimeMillis();
double avgSalary = employees.stream().map(Employee::getSalary).mapToDouble(i->i).average().getAsDouble();
end = System.currentTimeMillis();
System.out.println("Normal Stream Execution TIme: "+(end-start)+" avg salary: "+avgSalary);
start = System.currentTimeMillis();
double avgSalaryWithParallelStream = employees.stream().parallel().map(Employee::getSalary).mapToDouble(i->i).average().getAsDouble();
end = System.currentTimeMillis();
System.out.println("Parallel Stream Execution TIme: "+(end-start)+" avg salary: "+avgSalaryWithParallelStream);
start = System.currentTimeMillis();
double avgSalaryWithParallelStream2 = employees.parallelStream().map(Employee::getSalary).mapToDouble(i->i).average().getAsDouble();
end = System.currentTimeMillis();
System.out.println("Parallel Stream 2 Execution TIme: "+(end-start)+" avg salary: "+avgSalaryWithParallelStream2);
}
}