Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4eaa2ec

Browse filesBrowse files
authored
CompletableFuture Details
1. Add : thenCombine 2. Add : thenCompose 3. Add : acceptEither 4. Add : thenAcceptBoth 5. Add : anyOf 6. Add : allOf
1 parent b4836bf commit 4eaa2ec
Copy full SHA for 4eaa2ec

File tree

Expand file treeCollapse file tree

1 file changed

+171
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+171
-0
lines changed
Open diff view settings
Collapse file
+171Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
package info.doula.concurrency;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.concurrent.CompletableFuture;
6+
import java.util.concurrent.ExecutionException;
7+
import java.util.function.Supplier;
8+
9+
public class CompletableFutureDetails {
10+
CompletableFutureDetails app = new CompletableFutureDetails();
11+
12+
private static ArrayList<String> numbers = new ArrayList<String>(Arrays.asList("0", "1", "2"));
13+
14+
public static void main(String[] args) throws InterruptedException, ExecutionException {
15+
16+
testCompose();
17+
testCombine();
18+
testAcceptBoth();
19+
testAcceptEither();
20+
testAllOf();
21+
testAnyOf();
22+
}
23+
24+
private static void testAnyOf() throws InterruptedException, ExecutionException {
25+
System.out.println("==========================");
26+
System.out.println("Requesting Testing AnyOf");
27+
28+
CompletableFuture<String> future0 = createCFLongTime(0);
29+
CompletableFuture<String> future1 = createCF(1);
30+
CompletableFuture<String> future2 = createCFLongTime(2);
31+
32+
CompletableFuture<Object> future = CompletableFuture.anyOf(future0, future1, future2);
33+
34+
// check results
35+
System.out.println("Future result>> " + future.get());
36+
System.out.println(
37+
"All combined Futures result>> " + future0.get() + " | " + future1.get() + " | " + future2.get());
38+
}
39+
40+
private static void testAllOf() throws InterruptedException, ExecutionException {
41+
System.out.println("==========================");
42+
System.out.println("Requesting Testing AllOf");
43+
44+
CompletableFuture<String> future0 = createCF(0);
45+
CompletableFuture<String> future1 = createCFLongTime(1);
46+
CompletableFuture<String> future2 = createCF(2);
47+
48+
boolean isDone = CompletableFuture.allOf(future0, future1, future2).isDone();
49+
50+
// check results
51+
System.out.println("All futures are completed now>> " + isDone);
52+
System.out.println("Future result>> " + future0.get() + " | " + future1.get() + " | " + future2.get());
53+
}
54+
55+
private static void testAcceptEither() throws InterruptedException, ExecutionException {
56+
System.out.println("==========================");
57+
System.out.println("Requesting Testing Accept Either");
58+
59+
CompletableFuture<String> future = createCFLongTime(1);
60+
CompletableFuture<String> newFuture = createCF(2);
61+
62+
future.acceptEither(newFuture, s -> {
63+
System.out.println("the future which finishs first is: " + s);
64+
});
65+
66+
// check results
67+
System.out.println("Future result>> " + newFuture.get() + " and " + future.get());
68+
}
69+
70+
private static void testAcceptBoth() throws InterruptedException, ExecutionException {
71+
System.out.println("==========================");
72+
System.out.println("Requesting Testing Accept Both");
73+
74+
CompletableFuture<String> future = createCF(1); // future 1
75+
CompletableFuture<String> newFuture = createCFLongTime(2); // future 2
76+
77+
newFuture.thenAcceptBoth(future, CompletableFutureDetails::showAcceptBoth);
78+
79+
// check results
80+
System.out.println("Future result: " + newFuture.get() + " and " + future.get());
81+
}
82+
83+
private static void showAcceptBoth(String a, String b) {
84+
System.out.println("Accept Both: now finish both Futures>> " + a + " and " + b);
85+
}
86+
87+
private static void testCombine() throws InterruptedException, ExecutionException {
88+
System.out.println("==========================");
89+
System.out.println("Requesting Testing ComBine");
90+
91+
CompletableFuture<String> future = createCFLongTime(1);
92+
CompletableFuture<String> newFuture = createCF(2);
93+
94+
CompletableFuture<String> combinedFuture = future.thenCombine(newFuture, CompletableFutureDetails::appendString);
95+
96+
// check results
97+
System.out.println("Future result>> " + future.get());
98+
System.out.println("newFuture result>> " + newFuture.get());
99+
System.out.println("combinedFuture result>> " + combinedFuture.get());
100+
}
101+
102+
private static String appendString(String a, String b) {
103+
return a + " now appended to " + b;
104+
}
105+
106+
private static void testCompose() throws InterruptedException, ExecutionException {
107+
System.out.println("==========================");
108+
System.out.println("Requesting Testing Compose");
109+
110+
CompletableFuture<String> future = createCF(2);
111+
112+
CompletableFuture<String> combinedFuture = future.thenCompose(CompletableFutureDetails::calculateCF);
113+
114+
combinedFuture.thenAccept(result -> System.out.println("accept: " + result));
115+
116+
// check results
117+
System.out.println("Future result>> " + future.get());
118+
System.out.println("combinedFuture result>> " + combinedFuture.get());
119+
}
120+
121+
private static CompletableFuture<String> createCF(int index) {
122+
return CompletableFuture.supplyAsync(new Supplier<String>() {
123+
@Override
124+
public String get() {
125+
try {
126+
System.out.println("inside future: waiting for detecting index: " + index + "...");
127+
System.out.println("inside future: done...");
128+
129+
return numbers.get(index);
130+
} catch (Throwable e) {
131+
return "not detected";
132+
}
133+
}
134+
});
135+
}
136+
137+
private static CompletableFuture<String> createCFLongTime(int index) {
138+
return CompletableFuture.supplyAsync(new Supplier<String>() {
139+
@Override
140+
public String get() {
141+
try {
142+
System.out.println("inside future: waiting for detecting index: " + index + "...");
143+
for (int i = 1; i <= 5; i++) {
144+
try {
145+
Thread.sleep(500);
146+
} catch (InterruptedException e) {
147+
e.printStackTrace();
148+
}
149+
System.out.println("running inside Future " + index + "... " + i + " sec");
150+
}
151+
System.out.println("inside future: done...");
152+
153+
return numbers.get(index);
154+
} catch (Throwable e) {
155+
return "not detected";
156+
}
157+
}
158+
});
159+
}
160+
161+
private static CompletableFuture<String> calculateCF(String s) {
162+
163+
return CompletableFuture.supplyAsync(new Supplier<String>() {
164+
@Override
165+
public String get() {
166+
System.out.println("> inside new Future");
167+
return "new Completable Future: " + s;
168+
}
169+
});
170+
}
171+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.