2016-12-21 11:06:49 -08:00
|
|
|
// concurrent/DualCompletableOperations.java
|
2021-01-31 15:42:31 -07:00
|
|
|
// (c)2021 MindView LLC: see Copyright.txt
|
2016-12-21 11:06:49 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
|
|
|
// Visit http://OnJava8.com for more book information.
|
|
|
|
import java.util.concurrent.*;
|
2017-01-02 14:21:29 -08:00
|
|
|
import static onjava.CompletableUtilities.*;
|
2016-12-21 11:06:49 -08:00
|
|
|
|
|
|
|
public class DualCompletableOperations {
|
2016-12-25 12:36:49 -08:00
|
|
|
static CompletableFuture<Workable> cfA, cfB;
|
2016-12-21 11:06:49 -08:00
|
|
|
static void init() {
|
2017-01-22 16:48:11 -08:00
|
|
|
cfA = Workable.make("A", 0.15);
|
|
|
|
cfB = Workable.make("B", 0.10); // Always wins
|
2016-12-21 11:06:49 -08:00
|
|
|
}
|
|
|
|
static void join() {
|
|
|
|
cfA.join();
|
|
|
|
cfB.join();
|
2016-12-25 12:36:49 -08:00
|
|
|
System.out.println("*****************");
|
2016-12-21 11:06:49 -08:00
|
|
|
}
|
|
|
|
public static void main(String[] args) {
|
2016-12-25 12:36:49 -08:00
|
|
|
init();
|
2017-01-02 14:21:29 -08:00
|
|
|
voidr(cfA.runAfterEitherAsync(cfB, () ->
|
|
|
|
System.out.println("runAfterEither")));
|
2016-12-25 12:36:49 -08:00
|
|
|
join();
|
|
|
|
|
2016-12-21 11:06:49 -08:00
|
|
|
init();
|
2017-01-02 14:21:29 -08:00
|
|
|
voidr(cfA.runAfterBothAsync(cfB, () ->
|
|
|
|
System.out.println("runAfterBoth")));
|
2016-12-21 11:06:49 -08:00
|
|
|
join();
|
|
|
|
|
|
|
|
init();
|
2017-01-02 14:21:29 -08:00
|
|
|
showr(cfA.applyToEitherAsync(cfB, w -> {
|
2016-12-25 12:36:49 -08:00
|
|
|
System.out.println("applyToEither: " + w);
|
|
|
|
return w;
|
2017-01-02 14:21:29 -08:00
|
|
|
}));
|
2016-12-25 12:36:49 -08:00
|
|
|
join();
|
|
|
|
|
|
|
|
init();
|
2017-01-02 14:21:29 -08:00
|
|
|
voidr(cfA.acceptEitherAsync(cfB, w -> {
|
2016-12-25 12:36:49 -08:00
|
|
|
System.out.println("acceptEither: " + w);
|
2017-01-02 14:21:29 -08:00
|
|
|
}));
|
2016-12-25 12:36:49 -08:00
|
|
|
join();
|
|
|
|
|
|
|
|
init();
|
2017-01-02 14:21:29 -08:00
|
|
|
voidr(cfA.thenAcceptBothAsync(cfB, (w1, w2) -> {
|
2016-12-25 12:36:49 -08:00
|
|
|
System.out.println("thenAcceptBoth: "
|
|
|
|
+ w1 + ", " + w2);
|
2017-01-02 14:21:29 -08:00
|
|
|
}));
|
2016-12-21 11:06:49 -08:00
|
|
|
join();
|
|
|
|
|
|
|
|
init();
|
2017-01-02 14:21:29 -08:00
|
|
|
showr(cfA.thenCombineAsync(cfB, (w1, w2) -> {
|
2016-12-25 12:36:49 -08:00
|
|
|
System.out.println("thenCombine: "
|
|
|
|
+ w1 + ", " + w2);
|
|
|
|
return w1;
|
2017-01-02 14:21:29 -08:00
|
|
|
}));
|
2016-12-21 11:06:49 -08:00
|
|
|
join();
|
|
|
|
|
2016-12-25 12:36:49 -08:00
|
|
|
init();
|
|
|
|
CompletableFuture<Workable>
|
2017-01-22 16:48:11 -08:00
|
|
|
cfC = Workable.make("C", 0.08),
|
|
|
|
cfD = Workable.make("D", 0.09);
|
2016-12-25 12:36:49 -08:00
|
|
|
CompletableFuture.anyOf(cfA, cfB, cfC, cfD)
|
|
|
|
.thenRunAsync(() ->
|
|
|
|
System.out.println("anyOf"));
|
|
|
|
join();
|
|
|
|
|
|
|
|
init();
|
2017-01-22 16:48:11 -08:00
|
|
|
cfC = Workable.make("C", 0.08);
|
|
|
|
cfD = Workable.make("D", 0.09);
|
2016-12-25 12:36:49 -08:00
|
|
|
CompletableFuture.allOf(cfA, cfB, cfC, cfD)
|
|
|
|
.thenRunAsync(() ->
|
|
|
|
System.out.println("allOf"));
|
|
|
|
join();
|
2016-12-21 11:06:49 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Output:
|
2016-12-25 12:36:49 -08:00
|
|
|
Workable[BW]
|
|
|
|
runAfterEither
|
|
|
|
Workable[AW]
|
|
|
|
*****************
|
|
|
|
Workable[BW]
|
|
|
|
Workable[AW]
|
|
|
|
runAfterBoth
|
2017-01-02 14:21:29 -08:00
|
|
|
*****************
|
2016-12-25 12:36:49 -08:00
|
|
|
Workable[BW]
|
|
|
|
applyToEither: Workable[BW]
|
2017-01-02 14:21:29 -08:00
|
|
|
Workable[BW]
|
2016-12-25 12:36:49 -08:00
|
|
|
Workable[AW]
|
|
|
|
*****************
|
|
|
|
Workable[BW]
|
|
|
|
acceptEither: Workable[BW]
|
|
|
|
Workable[AW]
|
|
|
|
*****************
|
|
|
|
Workable[BW]
|
|
|
|
Workable[AW]
|
|
|
|
thenAcceptBoth: Workable[AW], Workable[BW]
|
2017-01-02 14:21:29 -08:00
|
|
|
*****************
|
2016-12-25 12:36:49 -08:00
|
|
|
Workable[BW]
|
|
|
|
Workable[AW]
|
|
|
|
thenCombine: Workable[AW], Workable[BW]
|
2017-01-02 14:21:29 -08:00
|
|
|
Workable[AW]
|
|
|
|
*****************
|
2016-12-25 12:36:49 -08:00
|
|
|
Workable[CW]
|
|
|
|
anyOf
|
2017-05-10 11:45:39 -06:00
|
|
|
Workable[DW]
|
2016-12-25 12:36:49 -08:00
|
|
|
Workable[BW]
|
|
|
|
Workable[AW]
|
|
|
|
*****************
|
2017-05-10 11:45:39 -06:00
|
|
|
Workable[CW]
|
2017-01-02 14:21:29 -08:00
|
|
|
Workable[DW]
|
2017-05-03 12:00:00 -06:00
|
|
|
Workable[BW]
|
2016-12-25 12:36:49 -08:00
|
|
|
Workable[AW]
|
|
|
|
*****************
|
2017-05-10 11:45:39 -06:00
|
|
|
allOf
|
2016-12-21 11:06:49 -08:00
|
|
|
*/
|