草庐IT

CompletableFuture

全部标签

java - 可完成的 future : Waiting for first one normally return?

我有一些CompletableFuture,我想并行运行它们,等待第一个正常返回。我知道我可以使用CompletableFuture.anyOf等待第一个返回,但这将返回正常或异常。我想忽略异常。List>futures=names.stream().map((Stringname)->CompletableFuture.supplyAsync(()->//thiscallingmaythrowexceptions.newTask(name).run())).collect(Collectors.toList());//FIXMECannotignoreexceptionallyret

java - 可完成的 future : Waiting for first one normally return?

我有一些CompletableFuture,我想并行运行它们,等待第一个正常返回。我知道我可以使用CompletableFuture.anyOf等待第一个返回,但这将返回正常或异常。我想忽略异常。List>futures=names.stream().map((Stringname)->CompletableFuture.supplyAsync(()->//thiscallingmaythrowexceptions.newTask(name).run())).collect(Collectors.toList());//FIXMECannotignoreexceptionallyret

JavaGuide知识点整理——CompletableFuture入门

其实CompletableFuture现在使用的比较多。很多开源框架都大量用到了。因此专门写一篇文章来介绍这个java8才被引入的一个非常有用的异步编程的类。CompletableFuture简介publicclassCompletableFutureimplementsFuture,CompletionStageCompletableFuture同时实现了Future和CompletionStage接口。除了提供了更为好用和强大的Future特性之外,还提供了函数式编程的能力。先从Future开始说起:Future是个接口,比较简洁,有五个方法:Future接口cancel(booleanm

java - CompletableFuture supplyAsync

我刚刚开始探索Java8的一些并发特性。让我有点困惑的一件事是这两个静态方法:CompletableFuturerunAsync(Runnablerunnable)CompletableFuturesupplyAsync(Suppliersupplier)有谁知道他们为什么选择使用接口(interface)供应商?使用Callable是不是更自然,类比Runnable返回值?那是因为供应商没有抛出无法处理的异常吗? 最佳答案 简答不,使用Callable不是更自然而不是Supplier在CompletableFuture.suppl

java - CompletableFuture supplyAsync

我刚刚开始探索Java8的一些并发特性。让我有点困惑的一件事是这两个静态方法:CompletableFuturerunAsync(Runnablerunnable)CompletableFuturesupplyAsync(Suppliersupplier)有谁知道他们为什么选择使用接口(interface)供应商?使用Callable是不是更自然,类比Runnable返回值?那是因为供应商没有抛出无法处理的异常吗? 最佳答案 简答不,使用Callable不是更自然而不是Supplier在CompletableFuture.suppl

java - 从 CompletableFuture 抛出异常

我有以下代码://HowtothrowtheServerException?publicvoidmyFunc()throwsServerException{//SomecodeCompletableFuturea=CompletableFuture.supplyAsync(()->{try{returnsomeObj.someFunc();}catch(ServerExceptionex){//throwex;givesanerrorhere.}}));//Somecode}someFunc()抛出ServerException。我不想在这里处理这个问题,而是将someFunc()的异

java - 从 CompletableFuture 抛出异常

我有以下代码://HowtothrowtheServerException?publicvoidmyFunc()throwsServerException{//SomecodeCompletableFuturea=CompletableFuture.supplyAsync(()->{try{returnsomeObj.someFunc();}catch(ServerExceptionex){//throwex;givesanerrorhere.}}));//Somecode}someFunc()抛出ServerException。我不想在这里处理这个问题,而是将someFunc()的异

java - Java 8 CompletableFuture 中的默认值超时

假设我有一些异步计算,例如:CompletableFuture.supplyAsync(()->createFoo()).thenAccept(foo->doStuffWithFoo(foo));如果异步供应商根据某些指定的超时超时,是否有一种很好的方法可以为foo提供默认值?理想情况下,此类功能也会尝试取消运行缓慢的供应商。例如,是否有类似于以下假设代码的标准库功能:CompletableFuture.supplyAsync(()->createFoo()).acceptEither(CompletableFuture.completedAfter(50,TimeUnit.MILLI

java - Java 8 CompletableFuture 中的默认值超时

假设我有一些异步计算,例如:CompletableFuture.supplyAsync(()->createFoo()).thenAccept(foo->doStuffWithFoo(foo));如果异步供应商根据某些指定的超时超时,是否有一种很好的方法可以为foo提供默认值?理想情况下,此类功能也会尝试取消运行缓慢的供应商。例如,是否有类似于以下假设代码的标准库功能:CompletableFuture.supplyAsync(()->createFoo()).acceptEither(CompletableFuture.completedAfter(50,TimeUnit.MILLI

java - ExecutorService.submit(Task) vs CompletableFuture.supplyAsync(Task, Executor)

要并行或异步运行一些东西,我可以使用ExecutorService:Futuresubmit(Runnabletask,Tresult);或CompletableFutureAPI:staticCompletableFuturesupplyAsync(Suppliersupplier,Executorexecutor);(假设我在这两种情况下都使用同一个Executor)除了返回类型Future与CompletableFuture有什么显着差异。或者什么时候用什么?如果我使用CompletableFuture有什么区别?默认APIExecutor(没有执行者的方法)?