-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallelStream.java
More file actions
43 lines (32 loc) · 1.33 KB
/
Copy pathparallelStream.java
File metadata and controls
43 lines (32 loc) · 1.33 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
import java.util.*;
public class Demo {
public static void main(String [] args){
int size = 1000;
List<Integer> nums = new ArrayList<>(size);
Random ran = new Random();
for(int i=1;i<=size;i++)
nums.add(ran.nextInt(100));
// int sum1 = nums.stream()
// .map(i -> i*2)
// .reduce(0,(c,e) -> c+e);
long startSeq = System.currentTimeMillis();
int sum2 = nums.stream()
.map(i -> {
try{Thread.sleep(1); }catch (Exception e){}
return i*2;})
.mapToInt(i->i)
.sum();
long endSeq = System.currentTimeMillis();
long startPara = System.currentTimeMillis();
int sum3 = nums.parallelStream()
.map(i -> {
try{Thread.sleep(1); }catch (Exception e){}
return i*2;})
.mapToInt(i->i)
.sum();
long endPara = System.currentTimeMillis();
// System.out.println(sum1);
System.out.println(endSeq-startSeq);
System.out.println(endPara-startPara);
}
}