Wednesday, September 14, 2022
HomeWordPress DevelopmentEnhance Efficiency with CompletableFuture in Java

Enhance Efficiency with CompletableFuture in Java


Repository : https://github.com/jorgetovar/completable_futures

Are you aware that parallel stream in Java has efficiency limitations resulting from Java will use the variety of obtainable processors?

If you wish to take advance of the most recent Java options and overcome this drawback, use CompletableFuture.

You’ll be able to create a ThreadPool larger than the variety of obtainable processors and reap the efficiency enhancements.
CompletableFuture makes use of lambdas, and the code is expressive.



My machine has 8 processors

   @Check
    public void numberOfProcessors() {
        assertThat(availableProcessors, is(equalTo(8)));
    }
Enter fullscreen mode

Exit fullscreen mode



1 Second: CompletableFuture implementation has the most effective efficiency

    @Check
    public void completableFuture_whenBooksAreMoreThanNumberOfProcessors() {
        Executor executor = Executors.newFixedThreadPool(10);

        lengthy begin = System.currentTimeMillis();
        var futureCategories = getBooks()
                .map(e -> CompletableFuture.supplyAsync(() -> BookClassifier.apply(e), executor))
                .toList();

        var classes = futureCategories.stream()
                .map(CompletableFuture::be part of).toList();
        int timeInSeconds = getTimeInSeconds(begin);
        assertThat(classes.measurement(), is(equalTo(10)));
        assertThat(timeInSeconds, OrderingComparison.greaterThanOrEqualTo(1));
        assertThat(timeInSeconds, OrderingComparison.lessThanOrEqualTo(1));

    }
Enter fullscreen mode

Exit fullscreen mode



2 Seconds: Parallel Stream implementation has some limitations

As a result of our present limitation must course of 8 books first after which 2 extra

    @Check
public void parallelStream_whenBooksAreMoreThanNumberOfProcessors() {
        lengthy begin = System.currentTimeMillis();
        var classes = getBooks()
        .parallel()
        .map(BookClassifier::apply)
        .toList();
        int timeInSeconds = getTimeInSeconds(begin);

        assertThat(classes.measurement(), is(equalTo(10)));
        assertThat(timeInSeconds, OrderingComparison.greaterThanOrEqualTo(2));
        assertThat(timeInSeconds, OrderingComparison.lessThanOrEqualTo(2));
        }
Enter fullscreen mode

Exit fullscreen mode



1 Second: Parallel Stream implementation inside the limitations

As a result of we’re inside the restrict of the obtainable processors


    @Check
    public void parallelStream_whenBooksAreLessThanNumberOfProcessors() {
        int restrict = availableProcessors - 1;
        lengthy begin = System.currentTimeMillis();
        var classes = getBooks()
                .restrict(restrict)
                .parallel()
                .map(BookClassifier::apply)
                .toList();
        int timeInSeconds = getTimeInSeconds(begin);

        System.out.printf("The operation took %s mspercentn", timeInSeconds - begin);
        assertThat(classes.measurement(), is(equalTo(restrict)));
        assertThat(timeInSeconds, OrderingComparison.greaterThanOrEqualTo(1));
        assertThat(timeInSeconds, OrderingComparison.lessThanOrEqualTo(1));

    }
Enter fullscreen mode

Exit fullscreen mode



10 Seconds: Stream Synchronous implementation has the worst efficiency

   @Check
   public void stream_whenBooksAreLessThanNumberOfProcessors() {
        lengthy begin = System.currentTimeMillis();
        var classes = getBooks()
        .map(BookClassifier::apply).toList();

        int timeInSeconds = getTimeInSeconds(begin);
        assertThat(classes.measurement(), is(equalTo(10)));
        assertThat(timeInSeconds, OrderingComparison.greaterThanOrEqualTo(9));
        assertThat(timeInSeconds, OrderingComparison.lessThanOrEqualTo(10));
        System.out.printf("The stream operation took %s mspercentn", timeInSeconds);

        }
Enter fullscreen mode

Exit fullscreen mode

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments