3927 |
|
|
3928 |
|
/** |
3929 |
|
* A single CompletableFuture with many dependents. |
3930 |
+ |
* A demo of scalability - runtime is O(n). |
3931 |
|
*/ |
3932 |
|
public void testManyDependents() throws Throwable { |
3933 |
< |
final int n = 10_000; |
3933 |
> |
final int n = 1_000; |
3934 |
|
final CompletableFuture<Void> head = new CompletableFuture<>(); |
3934 |
– |
final CompletableFuture<Void> incomplete = new CompletableFuture<>(); |
3935 |
|
final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null); |
3936 |
|
final AtomicInteger count = new AtomicInteger(0); |
3937 |
|
for (int i = 0; i < n; i++) { |
3938 |
|
head.thenRun(() -> count.getAndIncrement()); |
3939 |
|
head.thenAccept((x) -> count.getAndIncrement()); |
3940 |
|
head.thenApply((x) -> count.getAndIncrement()); |
3941 |
+ |
|
3942 |
|
head.runAfterBoth(complete, () -> count.getAndIncrement()); |
3943 |
|
head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement()); |
3944 |
|
head.thenCombine(complete, (x, y) -> count.getAndIncrement()); |
3945 |
< |
head.runAfterEither(incomplete, () -> count.getAndIncrement()); |
3946 |
< |
head.acceptEither(incomplete, (x) -> count.getAndIncrement()); |
3947 |
< |
head.applyToEither(incomplete, (x) -> count.getAndIncrement()); |
3945 |
> |
complete.runAfterBoth(head, () -> count.getAndIncrement()); |
3946 |
> |
complete.thenAcceptBoth(head, (x, y) -> count.getAndIncrement()); |
3947 |
> |
complete.thenCombine(head, (x, y) -> count.getAndIncrement()); |
3948 |
> |
|
3949 |
> |
head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement()); |
3950 |
> |
head.acceptEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement()); |
3951 |
> |
head.applyToEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement()); |
3952 |
> |
new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement()); |
3953 |
> |
new CompletableFuture<Void>().acceptEither(head, (x) -> count.getAndIncrement()); |
3954 |
> |
new CompletableFuture<Void>().applyToEither(head, (x) -> count.getAndIncrement()); |
3955 |
|
} |
3956 |
|
head.complete(null); |
3957 |
< |
assertEquals(9 * n, count.get()); |
3957 |
> |
assertEquals(5 * 3 * n, count.get()); |
3958 |
|
} |
3959 |
|
|
3960 |
|
// static <U> U join(CompletionStage<U> stage) { |