GeXiangDong

精通Java、SQL、Spring的拼写,擅长Linux、Windows的开关机

0%

java8中实现类似javascript Promise的功能

java8中提供了类似javascript Promise方式的写法:

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import java.util.concurrent.*;
import java.util.*;
import java.util.stream.*;

public class TestPromise{

public static void main(String[] argvs) throws Exception{
// 简单的lambda表达式的例子
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
list.forEach(n -> System.out.println(n));
System.out.println("-------");
list.forEach(System.out::println);
System.out.println("-------");

//下面是2个异步执行,执行完毕后使用结果的例子
String s = CompletableFuture.supplyAsync(() -> {
System.out.println("enter s1");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("s1");
return "hello";
}).thenCombine(CompletableFuture.supplyAsync(() -> {
System.out.println("enter s2");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("s2");
return "world";
}), (s1, s2) -> {return s1 + " " + s2;} ).join();
System.out.println(s);
System.out.println("-------");

// 多个方法异步执行,结果通过操纵一个共有的Map来保存,待全部执行完毕后显示结果
Map<String, String> result = new HashMap<String, String>();
CompletableFuture.allOf(
CompletableFuture.supplyAsync(() -> {
System.out.println("enter s1");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("s1");
result.put("k1", "11111");
return "11";
}),
CompletableFuture.supplyAsync(() -> {
System.out.println("enter s2");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("s2");
result.put("k2", "22222");
return "22";
}),
CompletableFuture.supplyAsync(() -> {
System.out.println("enter s3");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("s3");
result.put("k3", "33333");
return "33";
})
).get();
//.get()方法会等待allOf中所有参数方法执行完毕
System.out.println(result.get("k1") + " " + result.get("k2") + " " + result.get("k3"));


System.out.println("\r\n-------------------\r\n");
// 改进一下第一个方法,使其更易读;
CompletableFuture<Map<String, Object>> cf1 = CompletableFuture.supplyAsync(() -> {
System.out.println("enter s1");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("s1");
Map<String, Object> map = new HashMap<String, Object>();
map.put("s1", "11111");
return map;
});
CompletableFuture<Map<String, Object>> cf2 = CompletableFuture.supplyAsync(() -> {
System.out.println("enter s2");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("s2");
Map<String, Object> map = new HashMap<String, Object>();
map.put("s2", "22222");
return map;
});
CompletableFuture<Map<String, Object>> cf3 = CompletableFuture.supplyAsync(() -> {
System.out.println("enter s3");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
Map<String, Object> map = new HashMap<String, Object>();
map.put("s3", "33333");
return map;
});
// 注释掉的这2行和上一个例子相同,不处理cf1,cf2,cf3的返回值
// CompletableFuture<Void> cf = CompletableFuture.allOf(cf1, cf2, cf3);
// cf.get();
// 把各个CompletableFuture的执行结果放到一起

// 把结果合并成一个List
//List<Map<String, Object>> resultList = Stream.of(cf1, cf2, cf3).map(CompletableFuture::join).collect(Collectors.toList());
//resultList.forEach(n -> System.out.println("\t---" + n));

// 把结果合并到一个map里
Map<String, Object> resultMap = new HashMap<String, Object>();
Stream.of(cf1, cf2, cf3).map(CompletableFuture::join).forEach(v -> resultMap.putAll(v));
System.out.println(resultMap);
}
}

参考:

CompletableFurure 可参考javadoc https://docs.oracle.com/javase/8/docs/api/index.html?java/util/concurrent/CompletableFuture.html

java的Lambda表达式介绍 http://blog.oneapm.com/apm-tech/226.html

http://www.baeldung.com/java-completablefuture