首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >CompletableFuture:等待第一个正常返回?

CompletableFuture:等待第一个正常返回?
EN

Stack Overflow用户
提问于 2015-11-25 17:47:30
回答 4查看 10.7K关注 0票数 25

我有一些CompletableFuture,我想并行运行它们,等待第一个返回的,通常是

我知道我可以使用CompletableFuture.anyOf来等待第一个返回,但这通常会返回或我想忽略异常。

代码语言:javascript
复制
List<CompletableFuture<?>> futures = names.stream().map(
  (String name) ->
    CompletableFuture.supplyAsync(
      () ->
        // this calling may throw exceptions.
        new Task(name).run()
    )
).collect(Collectors.toList());
//FIXME Can not ignore exceptionally returned takes.
Future any = CompletableFuture.anyOf(futures.toArray(new CompletableFuture<?>[]{}));
try {
    logger.info(any.get().toString());
} catch (Exception e) {
    e.printStackTrace();
}
EN

回答 4

Stack Overflow用户

发布于 2015-12-09 03:03:13

您可以使用以下辅助方法:

代码语言:javascript
复制
public static <T>
    CompletableFuture<T> anyOf(List<? extends CompletionStage<? extends T>> l) {

    CompletableFuture<T> f=new CompletableFuture<>();
    Consumer<T> complete=f::complete;
    l.forEach(s -> s.thenAccept(complete));
    return f;
}

您可以像这样使用它,以演示它将忽略先前的异常,但返回第一个提供的值:

代码语言:javascript
复制
List<CompletableFuture<String>> futures = Arrays.asList(
    CompletableFuture.supplyAsync(
        () -> { throw new RuntimeException("failing immediately"); }
    ),
    CompletableFuture.supplyAsync(
        () -> { LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(5));
          return "with 5s delay";
        }),
    CompletableFuture.supplyAsync(
        () -> { LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(10));
          return "with 10s delay";
        })
);
CompletableFuture<String> c = anyOf(futures);
logger.info(c.join());

这种解决方案的一个缺点是,如果所有期货都异常完成,它将永远不会完成。一个解决方案,如果有成功的计算,将提供第一个值,但如果根本没有成功的计算,则会异常失败,这是一个更复杂的解决方案:

代码语言:javascript
复制
public static <T>
    CompletableFuture<T> anyOf(List<? extends CompletionStage<? extends T>> l) {

    CompletableFuture<T> f=new CompletableFuture<>();
    Consumer<T> complete=f::complete;
    CompletableFuture.allOf(
        l.stream().map(s -> s.thenAccept(complete)).toArray(CompletableFuture<?>[]::new)
    ).exceptionally(ex -> { f.completeExceptionally(ex); return null; });
    return f;
}

它利用了这样一个事实,即allOf的异常处理程序仅在所有期货都已完成(异常或未完成)后才会被调用,并且未来只能完成一次(抛开obtrude…之类的特殊事情不谈)。当执行异常处理程序时,任何使用结果来完成未来的尝试都已经完成,因此,只有在之前没有成功完成的情况下,才会尝试异常地完成它。

它可以与第一个解决方案完全相同的方式使用,并且只有在所有计算失败时才会表现出不同的行为,例如:

代码语言:javascript
复制
List<CompletableFuture<String>> futures = Arrays.asList(
    CompletableFuture.supplyAsync(
        () -> { throw new RuntimeException("failing immediately"); }
    ),
    CompletableFuture.supplyAsync(
        // delayed to demonstrate that the solution will wait for all completions
        // to ensure it doesn't miss a possible successful computation
        () -> { LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(5));
            throw new RuntimeException("failing later"); }
    )
);
CompletableFuture<String> c = anyOf(futures);
try { logger.info(c.join()); }
catch(CompletionException ex) { logger.severe(ex.toString()); }

上面的例子使用了一个延迟,演示了当没有成功时,解决方案将等待所有的完成,而this example on ideone将演示稍后的成功将如何将结果转化为成功。请注意,由于Ideones缓存结果,您可能不会注意到延迟。

请注意,在所有期货都失败的情况下,不能保证将报告哪些异常。由于它在错误的情况下等待所有的完成,所以任何一个都可以到达最终结果。

票数 15
EN

Stack Overflow用户

发布于 2015-12-09 03:01:03

嗯,这是一种框架应该支持的方法。首先,我以为CompletionStage.applyToEither会做类似的事情,但事实证明并非如此。所以我想出了这个解决方案:

代码语言:javascript
复制
public static <U> CompletionStage<U> firstCompleted(Collection<CompletionStage<U>> stages) {
  final int count = stages.size();
  if (count <= 0) {
    throw new IllegalArgumentException("stages must not be empty");
  }
  final AtomicInteger settled = new AtomicInteger();
  final CompletableFuture<U> future = new CompletableFuture<U>();
  BiConsumer<U, Throwable> consumer = (val, exc) -> {
    if (exc == null) {
      future.complete(val);
    } else {
      if (settled.incrementAndGet() >= count) {
        // Complete with the last exception. You can aggregate all the exceptions if you wish.
        future.completeExceptionally(exc);
      }
    }
  };
  for (CompletionStage<U> item : stages) {
    item.whenComplete(consumer);
  }
  return future;
}

要查看它的实际效果,这里有一些用法:

代码语言:javascript
复制
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiConsumer;

public class Main {
  public static <U> CompletionStage<U> firstCompleted(Collection<CompletionStage<U>> stages) {
    final int count = stages.size();
    if (count <= 0) {
      throw new IllegalArgumentException("stages must not be empty");
    }
    final AtomicInteger settled = new AtomicInteger();
    final CompletableFuture<U> future = new CompletableFuture<U>();
    BiConsumer<U, Throwable> consumer = (val, exc) -> {
      if (exc == null) {
        future.complete(val);
      } else {
        if (settled.incrementAndGet() >= count) {
          // Complete with the last exception. You can aggregate all the exceptions if you wish.
          future.completeExceptionally(exc);
        }
      }
    };
    for (CompletionStage<U> item : stages) {
      item.whenComplete(consumer);
    }
    return future;
  }

  private static final ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor();

  public static <U> CompletionStage<U> delayed(final U value, long delay) {
    CompletableFuture<U> future = new CompletableFuture<U>();
    worker.schedule(() -> {
      future.complete(value);
    }, delay, TimeUnit.MILLISECONDS);
    return future;
  }
  public static <U> CompletionStage<U> delayedExceptionally(final Throwable value, long delay) {
    CompletableFuture<U> future = new CompletableFuture<U>();
    worker.schedule(() -> {
      future.completeExceptionally(value);
    }, delay, TimeUnit.MILLISECONDS);
    return future;
  }

  public static void main(String[] args) throws InterruptedException, ExecutionException {
    System.out.println("Started...");

    /*
    // Looks like applyToEither doesn't work as expected
    CompletableFuture<Integer> a = CompletableFuture.completedFuture(99);
    CompletableFuture<Integer> b = Main.<Integer>completedExceptionally(new Exception("Exc")).toCompletableFuture();
    System.out.println(b.applyToEither(a, x -> x).get()); // throws Exc
    */

    try {
      List<CompletionStage<Integer>> futures = new ArrayList<>();
      futures.add(Main.<Integer>delayedExceptionally(new Exception("Exception #1"), 100));
      futures.add(Main.<Integer>delayedExceptionally(new Exception("Exception #2"), 200));
      futures.add(delayed(1, 1000));
      futures.add(Main.<Integer>delayedExceptionally(new Exception("Exception #4"), 400));
      futures.add(delayed(2, 500));
      futures.add(Main.<Integer>delayedExceptionally(new Exception("Exception #5"), 600));
      Integer value = firstCompleted(futures).toCompletableFuture().get();
      System.out.println("Completed normally: " + value);
    } catch (Exception ex) {
      System.out.println("Completed exceptionally");
      ex.printStackTrace();
    }

    try {
      List<CompletionStage<Integer>> futures = new ArrayList<>();
      futures.add(Main.<Integer>delayedExceptionally(new Exception("Exception B#1"), 400));
      futures.add(Main.<Integer>delayedExceptionally(new Exception("Exception B#2"), 200));
      Integer value = firstCompleted(futures).toCompletableFuture().get();
      System.out.println("Completed normally: " + value);
    } catch (Exception ex) {
      System.out.println("Completed exceptionally");
      ex.printStackTrace();
    }

    System.out.println("End...");
  }

}
票数 2
EN

Stack Overflow用户

发布于 2021-08-05 15:02:29

对上面的代码做了一些修改,允许测试第一个结果是否是预期的。

代码语言:javascript
复制
public class MyTask implements Callable<String> {

    @Override
    public String call() throws Exception {
        int randomNum = ThreadLocalRandom.current().nextInt(5, 20 + 1);
        for (int i = 0; i < randomNum; i++) {
            TimeUnit.SECONDS.sleep(1);
        }
        return "MyTest" + randomNum;
    }
}


public class CompletableFutureUtils {

    private static <T> T resolve(FutureTask<T> futureTask) {
        try {
            futureTask.run();
            return futureTask.get();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static <V> boolean predicate(Predicate<V> predicate, V v) {
        try {
            return predicate.test(v);
        } catch (Exception e) {
            return false;
        }
    }

    public static <T> void cancel(List<FutureTask<T>> futureTasks) {
        if (futureTasks != null && futureTasks.isEmpty() == false) {
            futureTasks.stream().filter(f -> f.isDone() == false).forEach(f -> f.cancel(true));
        }
    }

    public static <V> CompletableFuture<V> supplyAsync(List<FutureTask<V>> futureTasks, Predicate<V> predicate) {
        return supplyAsync(futureTasks, predicate, null);
    }

    public static <V> CompletableFuture<V> supplyAsync(List<FutureTask<V>> futureTasks, Predicate<V> predicate,
            Executor executor) {
        final int count = futureTasks.size();
        final AtomicInteger settled = new AtomicInteger();
        final CompletableFuture<V> result = new CompletableFuture<V>();
        final BiConsumer<V, Throwable> action = (value, ex) -> {
            settled.incrementAndGet();
            if (result.isDone() == false) {
                if (ex == null) {
                    if (predicate(predicate, value)) {
                        result.complete(value);
                        cancel(futureTasks);
                    } else if (settled.get() >= count) {
                        result.complete(null);
                    }
                } else if (settled.get() >= count) {
                    result.completeExceptionally(ex);
                }
            }
        };
        for (FutureTask<V> futureTask : futureTasks) {
            if (executor != null) {
                CompletableFuture.supplyAsync(() -> resolve(futureTask), executor).whenCompleteAsync(action, executor);
            } else {
                CompletableFuture.supplyAsync(() -> resolve(futureTask)).whenCompleteAsync(action);
            }
        }
        return result;
    }
}

public class DemoApplication {
    public static void main(String[] args) {
        List<FutureTask<String>> tasks = new ArrayList<FutureTask<String>>();
        for (int i = 0; i < 2; i++) {
            FutureTask<String> task = new FutureTask<String>(new MyTask());
            tasks.add(task);
        }
        Predicate<String> test = (s) -> true;
        CompletableFuture<String> result = CompletableFutureUtils.supplyAsync(tasks, test);
        try {
            String s = result.get(20, TimeUnit.SECONDS);
            System.out.println("result=" + s);
        } catch (Exception e) {
            e.printStackTrace();
            CompletableFutureUtils.cancel(tasks);
        }
    }
}

调用CompletableFutureUtils.cancel(tasks);非常重要,因为当超时发生时,它将取消后台任务。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33913193

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档