-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreamTestFuture.java
52 lines (48 loc) · 1.6 KB
/
StreamTestFuture.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
52
package chapter03;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* 3.4 JDK8 Stream & CompletableFuture
*
* @author hochenchong
* @date 2024/6/6
*/
public class StreamTestFuture {
private static String rpcCall(String ip, String param) {
System.out.println(ip + " rpcCall:" + param);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return param;
}
public static void main(String[] args) {
// 生成 ip 列表
List<String> ipList = new ArrayList<>();
for (int i = 1; i <= 10; i++) {
ipList.add("192.168.0." + i);
}
long start = System.currentTimeMillis();
// 发起广播调用
List<String> result = new ArrayList<>();
for (String ip : ipList) {
result.add(rpcCall(ip, ip));
}
// 输出
result.forEach(System.out::println);
System.out.println(System.currentTimeMillis() - start);
// 使用并发调用
System.out.println();
System.out.println("------");
start = System.currentTimeMillis();
List<CompletableFuture<String>> futures = ipList.stream().map(ip -> CompletableFuture.supplyAsync(() -> rpcCall(ip, ip)))
.toList();
// 等待所有异步任务执行完毕
result = futures.stream().map(CompletableFuture::join).toList();
// 输出
result.forEach(System.out::println);
System.out.println(System.currentTimeMillis() - start);
}
}