An Interest In:
Web News this Week
- April 2, 2024
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
September 13, 2022 10:09 pm GMT
Original Link: https://dev.to/jorgetovar/improve-performance-with-completablefuture-in-java-1jb6
Improve Performance with CompletableFuture in Java
Completable Futures
Repository : https://github.com/jorgetovar/completable_futures
Do you know that parallel stream in Java has performance limitations due to Java will use the number of available processors?
If you want to take advance of the latest Java features and overcome this problem, use CompletableFuture.
You can create a ThreadPool greater than the number of available processors and reap the performance improvements.
CompletableFuture uses lambdas, and the code is expressive.
My machine has 8 processors
@Test public void numberOfProcessors() { assertThat(availableProcessors, is(equalTo(8))); }
1 Second: CompletableFuture implementation has the best performance
@Test public void completableFuture_whenBooksAreMoreThanNumberOfProcessors() { Executor executor = Executors.newFixedThreadPool(10); long start = System.currentTimeMillis(); var futureCategories = getBooks() .map(e -> CompletableFuture.supplyAsync(() -> BookClassifier.apply(e), executor)) .toList(); var categories = futureCategories.stream() .map(CompletableFuture::join).toList(); int timeInSeconds = getTimeInSeconds(start); assertThat(categories.size(), is(equalTo(10))); assertThat(timeInSeconds, OrderingComparison.greaterThanOrEqualTo(1)); assertThat(timeInSeconds, OrderingComparison.lessThanOrEqualTo(1)); }
2 Seconds: Parallel Stream implementation has some limitations
Because our current limitation would have to process 8 books first and then 2 more
@Testpublic void parallelStream_whenBooksAreMoreThanNumberOfProcessors() { long start = System.currentTimeMillis(); var categories = getBooks() .parallel() .map(BookClassifier::apply) .toList(); int timeInSeconds = getTimeInSeconds(start); assertThat(categories.size(), is(equalTo(10))); assertThat(timeInSeconds, OrderingComparison.greaterThanOrEqualTo(2)); assertThat(timeInSeconds, OrderingComparison.lessThanOrEqualTo(2)); }
1 Second: Parallel Stream implementation within the limitations
Because we are within the limit of the available processors
@Test public void parallelStream_whenBooksAreLessThanNumberOfProcessors() { int limit = availableProcessors - 1; long start = System.currentTimeMillis(); var categories = getBooks() .limit(limit) .parallel() .map(BookClassifier::apply) .toList(); int timeInSeconds = getTimeInSeconds(start); System.out.printf("The operation took %s ms%n", timeInSeconds - start); assertThat(categories.size(), is(equalTo(limit))); assertThat(timeInSeconds, OrderingComparison.greaterThanOrEqualTo(1)); assertThat(timeInSeconds, OrderingComparison.lessThanOrEqualTo(1)); }
10 Seconds: Stream Synchronous implementation has the worst performance
@Test public void stream_whenBooksAreLessThanNumberOfProcessors() { long start = System.currentTimeMillis(); var categories = getBooks() .map(BookClassifier::apply).toList(); int timeInSeconds = getTimeInSeconds(start); assertThat(categories.size(), is(equalTo(10))); assertThat(timeInSeconds, OrderingComparison.greaterThanOrEqualTo(9)); assertThat(timeInSeconds, OrderingComparison.lessThanOrEqualTo(10)); System.out.printf("The stream operation took %s ms%n", timeInSeconds); }
Original Link: https://dev.to/jorgetovar/improve-performance-with-completablefuture-in-java-1jb6
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To