forked from coderbruis/JavaSourceCodeLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompletableFutureDemo.java
More file actions
220 lines (203 loc) · 6.98 KB
/
CompletableFutureDemo.java
File metadata and controls
220 lines (203 loc) · 6.98 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package com.learnjava.concurrent;
import org.junit.jupiter.api.Test;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CompletableFutureDemo {
/**
* completedFuture():
*
* 该方法会返回一个已经完成的带有返回值的CompletableFuture
*
* @throws Exception
*/
@Test
public void test_completed_future() throws Exception {
String expectedValue = "the expected value";
CompletableFuture<String> alreadyCompleted = CompletableFuture.completedFuture(expectedValue);
System.out.println(alreadyCompleted.get());
}
/**
* runAsync():
* 异步运行
*
*/
@Test
public void runAsyncExample() throws Exception {
ExecutorService executorService = Executors.newSingleThreadExecutor();
CompletableFuture cf = CompletableFuture.runAsync(() -> {
try {
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
}, executorService);
System.out.println(Thread.currentThread().getName());
while (true) {
if (cf.isDone()) {
System.out.println("CompletedFuture...isDown");
break;
}
}
}
/**
* 1. supplyAsync()
* 如果没有指定线程池,supplyAsync()方法会通过ForkJoinPool.commonPool()中的线程来
* 异步完成任务,并通过给定Supplier获得值。
* 2. thenAccept()
* 接收CompletableFuture结果并进行处理。
* 3. thenAcceptAsync()
* 异步接收CompletableFuture结果
* @throws Exception
*/
@Test
public void thenApply() throws Exception {
ExecutorService executorService = Executors.newFixedThreadPool(2);
/*Supplier<String> sp = () -> {
return "";
};
Supplier<String> spp = String::new;*/
CompletableFuture cf = CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("supplyAsync " + Thread.currentThread().getName());
return "hello ";
},executorService).thenAccept(s -> {
try {
thenApply_test(s + "world");
} catch (Exception e) {
e.printStackTrace();
}
});
System.out.println(Thread.currentThread().getName());
while (true) {
if (cf.isDone()) {
System.out.println("CompletedFuture...isDown");
break;
}
}
/** 运行结果:
main
supplyAsync pool-1-thread-1
thenApply_test hello world
thenApply_test pool-1-thread-1
*/
}
@Test
public void thenApplyAsync() throws Exception {
ExecutorService executorService = Executors.newFixedThreadPool(2);
CompletableFuture cf = CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("supplyAsync " + Thread.currentThread().getName());
return "hello ";
},executorService)/*.thenAcceptAsync(s -> {
try {
thenApply_test(s + "world");
} catch (Exception e) {
e.printStackTrace();
}
},executorService)*/;
System.out.println(Thread.currentThread().getName());
while (true) {
if (cf.isDone()) {
System.out.println("CompletedFuture...isDown result = " + cf.get());
break;
}
}
/** 运行结果:
main
supplyAsync pool-1-thread-1
thenApply_test hello world
thenApply_test pool-1-thread-2
*/
}
private void thenApply_test(String msg) {
System.out.println("thenApply_test " + msg);
System.out.println("thenApply_test " + Thread.currentThread().getName());
}
@Test
public void thenAcceptBoth() {
CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "hello";
}).thenAcceptBoth(CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "world";
}), (s1, s2) -> {
System.out.println("thenAcceptBoth " + Thread.currentThread().getName());
System.out.println(s1 + " " + s2);
}); // hello world
System.out.println(Thread.currentThread().getName());
while (true){}
}
@Test
public void exceptionally() throws Exception {
ExecutorService executorService = Executors.newSingleThreadExecutor();
CompletableFuture cf = CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (1 == 1) {
throw new RuntimeException("测试exceptionally...");
}
return "s1";
}, executorService).exceptionally(e -> {
System.out.println(e.getMessage());
return "helloworld " + e.getMessage();
});
cf.thenAcceptAsync(s -> {
System.out.println("thenAcceptAsync: " + s);
});
System.out.println("main: " + Thread.currentThread().getName());
while (true) {
if (cf.isDone()) {
System.out.println("CompletableFuture is Down..." + cf.get());
break;
}
}
}
/**
* join()方法会阻塞等待CompletableFuture获取完结果
*
* 可以看到supplyAsync使用的是线程池中的线程,而main线程会阻塞等待completableFuture任务完成。
*
*/
@Test
public void exceptionallyByJoin() {
ExecutorService executorService = Executors.newSingleThreadExecutor();
String result = CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
if (1 == 1) {
throw new RuntimeException("测试exceptionally...");
}
return "s1";
}, executorService).exceptionally(e -> {
System.out.println(e.getMessage());
return "helloworld " + e.getMessage();
}).join();
System.out.println(result + "," + Thread.currentThread().getName());
}
}