Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.alithya.platon;
- import static java.util.stream.Collectors.toList;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Objects;
- import java.util.concurrent.CompletableFuture;
- import java.util.concurrent.TimeUnit;
- import org.junit.jupiter.api.AfterEach;
- import org.junit.jupiter.api.BeforeEach;
- import org.junit.jupiter.api.Test;
- public class RandomTest {
- public static final List<String> strings = Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8");
- MyTest myTest = new MyTest();
- long millisBefore; // used to benchmark
- @BeforeEach
- void setUp() {
- millisBefore = System.currentTimeMillis();
- }
- @AfterEach
- void tearDown() {
- System.out.printf("time taken : %.4fs\n",
- (System.currentTimeMillis() - millisBefore) / 1000d);
- }
- @Test
- void oliExample() throws Exception { // 8.04s
- List<CompletableFuture<String>> listOfFutures = strings.stream()
- .map(myTest::downloadWebPage).collect(toList());
- CompletableFuture<List<String>> futureOfList = CompletableFuture
- .allOf(listOfFutures.toArray(new CompletableFuture[0]))
- .thenApply(
- v -> listOfFutures.stream().map(CompletableFuture::join).collect(toList()));
- System.out.println(futureOfList.get()); // blocks here
- }
- @Test
- void mySolution() throws Exception { // 8s
- var futures = myTest.getFilteredEventsFaster(strings);
- System.out.println(futures.get()); // blocks here
- }
- @Test
- void originalProblem() { // 32s
- var orig = strings.stream()
- .map(myTest::origDownload)
- .flatMap(List::stream)
- .collect(toList());
- System.out.println(orig.toString());
- }
- @Test
- void possiblyTrivial() { // 4s
- var orig = strings.parallelStream()
- .map(myTest::origDownload)
- .flatMap(List::stream)
- .collect(toList());
- System.out.println(orig.toString());
- }
- }
- class MyTest {
- List<String> origDownload(String webPageLink) {
- try {
- TimeUnit.SECONDS.sleep(4);
- } catch (Exception io) {
- throw new RuntimeException(io);
- } finally {
- return RandomTest.strings;
- }
- }
- CompletableFuture<String> downloadWebPage(String webPageLink) {
- return CompletableFuture.supplyAsync(() -> {
- try {
- TimeUnit.SECONDS.sleep(4);
- } catch (Exception io) {
- throw new RuntimeException(io);
- } finally {
- return "downloaded : " + webPageLink;
- }
- });
- }
- CompletableFuture<List<String>> getFilteredEventsFaster(List<String> strings) {
- /* Collecting the list of all the async requests that build a List<Event>. */
- List<CompletableFuture<List<String>>> completableFutures = strings.stream()
- .map(api -> getFilteredEventsAsync(strings))
- .collect(toList());
- /* Creating a single Future that contains all the Futures we just created ("flatmap"). */
- CompletableFuture<Void> allFutures = CompletableFuture.allOf(completableFutures
- .toArray(new CompletableFuture[strings.size()]));
- /* When all the Futures have completed, we join them to create merged List<Event>. */
- CompletableFuture<List<String>> allCompletableFutures = allFutures
- .thenApply(future -> completableFutures.stream()
- .filter(Objects::nonNull) // we filter out the failed calls
- .map(CompletableFuture::join)
- .flatMap(List::stream) // creating a List<Event> from List<List<Event>>
- .collect(toList())
- );
- return allCompletableFutures;
- }
- private static CompletableFuture<List<String>> getFilteredEventsAsync(List<String> strings) {
- /* Manage the Exceptions here to ensure the wrapping Future returns the other calls. */
- return CompletableFuture.supplyAsync(() -> {
- try {
- TimeUnit.SECONDS.sleep(4);
- return strings;
- } catch (Exception io) {
- throw new RuntimeException(io);
- }
- })
- .exceptionally(ex -> {
- return null; // gets managed in the wrapping Future
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement