Skip to content

Commit f7911fb

Browse files
committed
提交第七章代码
1 parent 8d04fdc commit f7911fb

4 files changed

Lines changed: 111 additions & 0 deletions

File tree

chap7/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>node</artifactId>
7+
<groupId>xin.codedream.java8</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>chap7</artifactId>
13+
14+
15+
</project>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package xin.codedream.java8.chap7;
2+
3+
import java.util.stream.LongStream;
4+
import java.util.stream.Stream;
5+
6+
/**
7+
* ParallelStreams
8+
*
9+
* @author NGLSL
10+
* @date 2018/10/6
11+
*/
12+
public class ParallelStreams {
13+
public static long sequentialSum(long n) {
14+
// 生成自然数无限流
15+
return Stream.iterate(1L, i -> i + 1)
16+
// 限制到前n个数
17+
.limit(n)
18+
// 对所有数字求和来归纳流
19+
.reduce(0L, Long::sum);
20+
}
21+
22+
public static long iterativeSum(long n) {
23+
long result = 0;
24+
for (long i = 0; i <= n; i++) {
25+
result += i;
26+
}
27+
return result;
28+
}
29+
30+
public static long parallelSum(long n) {
31+
// 生成自然数无限流
32+
return Stream.iterate(1L, i -> i + 1)
33+
// 限制到前n个数
34+
.limit(n)
35+
// 将流转为并行流
36+
.parallel()
37+
// 对所有数字求和来归纳流
38+
.reduce(0L, Long::sum);
39+
}
40+
41+
public static long rangedSum(long n) {
42+
return LongStream.rangeClosed(1, n)
43+
.reduce(0L, Long::sum);
44+
}
45+
46+
public static long parallelRangedSum(long n) {
47+
return LongStream.rangeClosed(1, n)
48+
.parallel()
49+
.reduce(0L, Long::sum);
50+
}
51+
52+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package xin.codedream.java8.chap7;
2+
3+
import java.util.function.Function;
4+
5+
/**
6+
* ParallelStreamsHarness
7+
*
8+
* @author NGLSL
9+
* @date 2018/10/6
10+
*/
11+
public class ParallelStreamsHarness {
12+
public static void main(String[] args) {
13+
/*System.out.println("Sequential sum done in:" +
14+
measureSumPerf(ParallelStreams::sequentialSum, 10_000_000) + " msecs");*/
15+
16+
/*System.out.println("Iterative sum done in:" +
17+
measureSumPerf(ParallelStreams::iterativeSum, 10_000_000) + " msecs");*/
18+
19+
/*System.out.println("Parallel sum done in: " +
20+
measureSumPerf(ParallelStreams::parallelSum, 10_000_000) + " msecs");*/
21+
22+
/*System.out.println("Ranged sum done in: " +
23+
measureSumPerf(ParallelStreams::rangedSum, 10_000_000) + " msecs");*/
24+
25+
System.out.println("Parallel range sum done in:" +
26+
measureSumPerf(ParallelStreams::parallelRangedSum, 10_000_000) +
27+
" msecs");
28+
}
29+
30+
public static long measureSumPerf(Function<Long, Long> adder, long n) {
31+
long fastest = Long.MAX_VALUE;
32+
for (int i = 0; i < 10; i++) {
33+
long start = System.nanoTime();
34+
long sum = adder.apply(n);
35+
long duration = (System.nanoTime() - start) / 1_000_000;
36+
System.out.println("Result: " + sum);
37+
if (duration < fastest) {
38+
fastest = duration;
39+
}
40+
}
41+
return fastest;
42+
}
43+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@
2727
<module>chap4</module>
2828
<module>chap5</module>
2929
<module>chap6</module>
30+
<module>chap7</module>
3031
</modules>
3132
</project>

0 commit comments

Comments
 (0)