高效 告别996,开启java高效编程之门 3-12实战:常用终端操作之查找

1    重点

1.1  findFirst,findAny方法使用

1.2  sequential(串行),parallel(并行)方法使用

1.3  iterate,reduce方法的使用

1.4  串行,并行的区别(详情见2)

1.5  findFirst,findAny方法的区别(详情见2)

1.6  reduce存疑(查看第7标题)

1.7  reduce方法简介

2    概念答疑

2.1  findFirst,findAny的区别

串行上无区别,并行上findAny执行效率更高,但是可能找到的并不是第一条

 

2.2  串行,并行的区别

 串行是最一般的情况,程序会按顺序执行每个任务,效率往往十分低下。与之相对的是并行,多个任务会同时运行在不同的cpu线程上,效率较高,但受限于cpu线程数,如果任务数量超过了CPU线程数,那么每个线程上的任务仍然是顺序执行的。而并发是指多个线程在宏观(相对于较长的时间区间而言)上表现为同时执行,而实际上是轮流穿插着执行,并发与并行串行并不是互斥的概念,如果是在一个CPU线程上启用并发,那么自然就还是串行的,而如果在多个线程上启用并发,那么程序的执行就可以是既并发又并行的。如下图:

https://www.meiwen.com.cn/subject/xgkonxtx.html

2.3  reduce方法简介

https://www.cnblogs.com/jzxs/p/11426694.html

3    Demo之findFirst:

demo:

@Test
    /**
     * 短路操作
     * findFirst和findAny 在串行上没有区别,在并行上findAny执行效率更高,但是找到的可能并不是第一条
     */
    public void findFirst(){
        Optional<Sku> optional = list.stream().findFirst();
        System.out.println(JSONObject.toJSONString(optional.get(),true));
    }

打印日志:

{
    "skuCategory":"ELECTRONICS",
    "skuId":2020001,
    "skuName":"无人机",
    "skuPrice":999.0,
    "totalNum":1,
    "totalPrice":999.0
}

Process finished with exit code 0
4    Demo之findAny:

 demo:

  @Test
    /**
     * 短路操作
     */
    public void findAny(){
        Optional<Sku> optional = list.stream().findAny();
        System.out.println(JSONObject.toJSONString(optional.get(),true));
    }

打印日志:

{
    "skuCategory":"ELECTRONICS",
    "skuId":2020001,
    "skuName":"无人机",
    "skuPrice":999.0,
    "totalNum":1,
    "totalPrice":999.0
}

Process finished with exit code 0
5    Demo之串行:

demo:

    /**
     * sequential() 串行,不带时,默认串行
     */
    @Test
    public void ChuanXing(){
        Stream.iterate(1, i -> i + 1)
                .limit(100)
                .peek(i->System.out.println(i))
                .sequential()
                .reduce(0, Integer::sum);
    }

打印日志:

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

Process finished with exit code 0
6    Demo之并行:

demo:

   /**
     * parallel() 并行
     */
    @Test
    public void BingXing(){
        Stream.iterate(1, i -> i + 1)
                .limit(100)
                .peek(i->System.out.println(i))
                .parallel()
                .reduce(0, Integer::sum);
    }

打印日志:

74
75
76
77
78
79
80
81
82
69
70
71
72
73
65
66
67
68
92
93
94
95
96
97
98
99
100
87
88
89
90
91
83
84
85
86
41
42
43
44
45
46
47
48
17
18
19
33
20
21
22
23
24
25
26
27
28
29
30
31
32
9
10
11
12
13
14
15
16
1
2
3
4
5
6
7
8
34
35
36
37
38
39
40
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

Process finished with exit code 0
7    Demo之并行不能使用的情况(存疑):

demo:(这个不能用并行,否则计算结果不准确)

reduce的这个方法不理解,已经提问了,等他回复

    @Test
    public void BingXingBug(){
        System.out.println(Stream.iterate(1, i -> i + 1)
                .limit(1000)
                .sequential()
                .reduce(0, (a, b) -> a - b));
    }

打印日志:

-500500

Process finished with exit code 0

猜你喜欢

转载自www.cnblogs.com/1446358788-qq/p/12799666.html