-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParallel.java
More file actions
38 lines (29 loc) · 876 Bytes
/
Copy pathParallel.java
File metadata and controls
38 lines (29 loc) · 876 Bytes
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
package com.jcfc.stream.parallel;
import org.junit.Test;
import java.time.Duration;
import java.time.Instant;
import java.util.stream.LongStream;
import java.util.stream.Stream;
/**
* java8并行流:
* (底层实现是 Fork/Join 框架)
*
* 切换并行流:
* parallel()
* list.parallelStream()
* 切换穿行流:
* sequential()
*
*/
public class Parallel {
@Test
public void test() {
Instant start = Instant.now();
//LongStream.rangeClosed生成在开始和结束之间所有连续的数,closed是包含结束,不带是不包含
long sum = LongStream.rangeClosed(0, 100000000L)
.parallel()
.reduce(0, Long::sum);
Instant end = Instant.now();
System.out.println("耗费时间:"+ Duration.between(start, end).toMillis());
}
}