forked from itguang/java8
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientTest.java
More file actions
194 lines (153 loc) · 5.15 KB
/
Copy pathClientTest.java
File metadata and controls
194 lines (153 loc) · 5.15 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import com.sun.org.apache.xpath.internal.SourceTree;
import completablefuture.DiscountShop;
import completablefuture.Shop;
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import static java.util.stream.Collectors.toList;
/**
* 让你的代码免受阻塞之苦
*
* @author itguang
* @create 2017-11-21 16:50
**/
public class ClientTest {
List<Shop> shops;
@Before
public void before() {
shops = Arrays.asList(new Shop("淘宝"),
new Shop("天猫"),
new Shop("京东"),
new Shop("亚马逊"),
new Shop("实体店")
);
}
/**
* 采用顺序查询所有商店的方式实现的 findPrices 方法
*
* @param product
* @return
*/
public List<String> findPrice(String product) {
List<String> list = shops.stream()
.map(shop ->
String.format("%s price is %.2f RMB",
shop.getName(),
shop.getPrice(product)))
.collect(toList());
return list;
}
/**
* 使用并行流对请求进行并行操作
*
* @param product
* @return
*/
public List<String> findPrice2(String product) {
List<String> list = shops.parallelStream()
.map(shop ->
String.format("%s price is %.2f RMB",
shop.getName(),
shop.getPrice(product)))
.collect(toList());
return list;
}
/**
* 使用 CompletableFuture 发起异步请求
*
* @param product
* @return
*/
public List<String> findPrice3(String product) {
List<CompletableFuture<String>> futures = shops.stream()
.map(shop -> CompletableFuture.supplyAsync(
() -> String.format("%s price is %.2f RMB",
shop.getName(),
shop.getPrice(product)))
)
.collect(toList());
List<String> list = futures.stream()
.map(CompletableFuture::join)
.collect(toList());
return list;
}
/**
* 使用定制的 Executor 配置 CompletableFuture
*
* @param product
* @return
*/
public List<String> findPrice4(String product) {
//为“最优价格查询器”应用定制的执行器 Execotor
Executor executor = Executors.newFixedThreadPool(Math.min(shops.size(), 100),
new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r);
//使用守护线程,使用这种方式不会组织程序的关停
thread.setDaemon(true);
return thread;
}
}
);
//将执行器Execotor 作为第二个参数传递给 supplyAsync 工厂方法
List<CompletableFuture<String>> futures = shops.stream()
.map(shop -> CompletableFuture.supplyAsync(
() -> String.format("%s price is %.2f RMB",
shop.getName(),
shop.getPrice(product)), executor)
)
.collect(toList());
List<String> list = futures.stream()
.map(CompletableFuture::join)
.collect(toList());
return list;
}
/**
* 采用顺序查询所有商店的方式实现的 findPrices 方法,查询每个商店里的 iphone666s
*/
@Test
public void test() {
long start = System.nanoTime();
List<String> list = findPrice("iphone666s");
System.out.println(list);
System.out.println("Done in " + (System.nanoTime() - start) / 1_000_000 + " ms");
}
/**
* 使用并行流对请求进行并行操作
*/
@Test
public void test2() {
long start = System.nanoTime();
List<String> list = findPrice2("iphone666s");
System.out.println(list);
System.out.println("Done in " + (System.nanoTime() - start) / 1_000_000 + " ms");
}
/**
* 使用 CompletableFuture 发起异步请求
*/
@Test
public void test3() {
long start = System.nanoTime();
List<String> list = findPrice3("iphone666s");
System.out.println(list);
System.out.println("Done in " + (System.nanoTime() - start) / 1_000_000 + " ms");
}
/**
* 使用定制的 Executor 配置 CompletableFuture
*/
@Test
public void test4() {
long start = System.nanoTime();
List<String> list = findPrice4("iphone666s");
System.out.println(list);
System.out.println("Done in " + (System.nanoTime() - start) / 1_000_000 + " ms");
}
}