|
| 1 | +package io.foldright.demo; |
| 2 | + |
| 3 | +import io.foldright.cffu2.CfParallelUtils; |
| 4 | +import io.foldright.cffu2.CffuFactory; |
| 5 | +import io.foldright.cffu2.CompletableFutureUtils; |
| 6 | +import io.foldright.cffu2.MCffu; |
| 7 | + |
| 8 | +import java.util.List; |
| 9 | +import java.util.concurrent.CompletableFuture; |
| 10 | +import java.util.concurrent.ExecutorService; |
| 11 | +import java.util.concurrent.Executors; |
| 12 | +import java.util.function.Function; |
| 13 | + |
| 14 | +import static io.foldright.test_utils.TestUtils.sleep; |
| 15 | +import static java.util.Arrays.asList; |
| 16 | +import static java.util.concurrent.CompletableFuture.completedFuture; |
| 17 | + |
| 18 | + |
| 19 | +/** |
| 20 | + * This shows the usage of Multiple Actions methods(<b>M</b> Methods). |
| 21 | + * <p> |
| 22 | + * Run by maven: {@code |
| 23 | + * mvn -pl cffu-core test-compile exec:exec -Dexec.mainClass=io.foldright.demo.MultipleActionsDemo |
| 24 | + * } |
| 25 | + */ |
| 26 | +public class CfParallelDemo { |
| 27 | + private static final ExecutorService myBizExecutor = Executors.newCachedThreadPool(); |
| 28 | + private static final CffuFactory cffuFactory = CffuFactory.builder(myBizExecutor).build(); |
| 29 | + |
| 30 | + public static void main(String[] args) { |
| 31 | + parApplyFailFastAsyncDemo(); |
| 32 | + thenParApplyFailFastAsyncDemo(); |
| 33 | + |
| 34 | + //////////////////////////////////////// |
| 35 | + // cleanup |
| 36 | + //////////////////////////////////////// |
| 37 | + myBizExecutor.shutdown(); |
| 38 | + } |
| 39 | + |
| 40 | + static void parApplyFailFastAsyncDemo() { |
| 41 | + Function<Integer, Integer> fn = x -> x + 1; |
| 42 | + final List<Integer> list = asList(42, 43, 44); |
| 43 | + |
| 44 | + // wrap data with action to CompletableFutures first, AWKWARD and COMPLEX! 😖 |
| 45 | + @SuppressWarnings("unchecked") |
| 46 | + CompletableFuture<Integer>[] cfs = new CompletableFuture[list.size()]; |
| 47 | + for (int i = 0; i < list.size(); i++) { |
| 48 | + Integer e = list.get(i); |
| 49 | + cfs[i] = CompletableFuture.supplyAsync(() -> fn.apply(e)); |
| 50 | + } |
| 51 | + CompletableFutureUtils.allResultsFailFastOf(cfs).thenAccept(System.out::println); |
| 52 | + // output: [43, 44, 45] |
| 53 | + cffuFactory.allResultsFailFastOf(cfs).thenAccept(System.out::println); |
| 54 | + // output: [43, 44, 45] |
| 55 | + |
| 56 | + // just process multiple data, fresh and cool 😋 |
| 57 | + CfParallelUtils.parApplyFailFastAsync( |
| 58 | + asList(42, 43, 44), |
| 59 | + x -> x + 1 |
| 60 | + ).thenAccept(System.out::println); |
| 61 | + // output: [43, 44, 45] |
| 62 | + cffuFactory.parOps().parApplyFailFastAsync( |
| 63 | + asList(42, 43, 44), |
| 64 | + x -> x + 1 |
| 65 | + ).thenAccept(System.out::println); |
| 66 | + // output: [43, 44, 45] |
| 67 | + |
| 68 | + sleep(1000); |
| 69 | + } |
| 70 | + |
| 71 | + static void thenParApplyFailFastAsyncDemo() { |
| 72 | + Function<Integer, Integer> fn = x -> x + 1; |
| 73 | + final CompletableFuture<List<Integer>> cf = completedFuture(asList(42, 43, 44)); |
| 74 | + |
| 75 | + // wrap data with action to CompletableFutures first, AWKWARD and COMPLEX! 😖 |
| 76 | + cf.thenCompose(list -> { |
| 77 | + @SuppressWarnings("unchecked") |
| 78 | + CompletableFuture<Integer>[] cfs = new CompletableFuture[list.size()]; |
| 79 | + for (int i = 0; i < list.size(); i++) { |
| 80 | + Integer e = list.get(i); |
| 81 | + cfs[i] = CompletableFuture.supplyAsync(() -> fn.apply(e)); |
| 82 | + } |
| 83 | + return CompletableFutureUtils.allResultsFailFastOf(cfs); |
| 84 | + }).thenAccept(System.out::println); |
| 85 | + // output: [43, 44, 45] |
| 86 | + final MCffu<Integer, List<Integer>> mCffu = cffuFactory.completedMCffu(asList(42, 43, 44)); |
| 87 | + mCffu.thenCompose(list -> { |
| 88 | + @SuppressWarnings("unchecked") |
| 89 | + CompletableFuture<Integer>[] cfs = new CompletableFuture[list.size()]; |
| 90 | + for (int i = 0; i < list.size(); i++) { |
| 91 | + Integer e = list.get(i); |
| 92 | + cfs[i] = CompletableFuture.supplyAsync(() -> fn.apply(e)); |
| 93 | + } |
| 94 | + return CompletableFutureUtils.allResultsFailFastOf(cfs); |
| 95 | + }).thenAccept(System.out::println); |
| 96 | + // output: [43, 44, 45] |
| 97 | + |
| 98 | + // just process multiple data, fresh and cool 😋 |
| 99 | + CfParallelUtils.thenParApplyFailFastAsync(cf, x -> x + 1) |
| 100 | + .thenAccept(System.out::println); |
| 101 | + // output: [43, 44, 45] |
| 102 | + mCffu.parOps().thenParApplyFailFastAsync(x -> x + 1) |
| 103 | + .thenAccept(System.out::println); |
| 104 | + // output: [43, 44, 45] |
| 105 | + |
| 106 | + sleep(1000); |
| 107 | + } |
| 108 | +} |
0 commit comments