Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8152924

Improve scalability of CompletableFuture with large number of dependents

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P3
    • 9
    • None
    • core-libs

    Description

      Here's a test where a future has many dependents, that should scale linearly with n:


          /**
           * A single CompletableFuture with many dependents.
           * A demo of scalability - runtime is O(n).
           */
          public void testManyDependents() throws Throwable {
              final int n = 1_000;
              final CompletableFuture<Void> head = new CompletableFuture<>();
              final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null);
              final AtomicInteger count = new AtomicInteger(0);
              for (int i = 0; i < n; i++) {
                  head.thenRun(() -> count.getAndIncrement());
                  head.thenAccept((x) -> count.getAndIncrement());
                  head.thenApply((x) -> count.getAndIncrement());

                  head.runAfterBoth(complete, () -> count.getAndIncrement());
                  head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement());
                  head.thenCombine(complete, (x, y) -> count.getAndIncrement());
                  complete.runAfterBoth(head, () -> count.getAndIncrement());
                  complete.thenAcceptBoth(head, (x, y) -> count.getAndIncrement());
                  complete.thenCombine(head, (x, y) -> count.getAndIncrement());

                  head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement());
                  head.acceptEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement());
                  head.applyToEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement());
                  new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement());
                  new CompletableFuture<Void>().acceptEither(head, (x) -> count.getAndIncrement());
                  new CompletableFuture<Void>().applyToEither(head, (x) -> count.getAndIncrement());
              }
              head.complete(null);
              assertEquals(5 * 3 * n, count.get());
          }

      Attachments

        Activity

          People

            martin Martin Buchholz
            martin Martin Buchholz
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: