Tag: completable future

如何为CompletableFuture :: supplyAsync选择Executor

CompletableFuture::supplyAsync(() -> IO bound queries) 如何为CompletableFuture :: supplyAsync选择Executor以避免污染ForkJoinPool.commonPool() 。 Executors有很多选项( newCachedThreadPool , newWorkStealingPool , newFixedThreadPool等) 我在这里阅读了有关新ForkJoinPool的内容 如何为我的用例选择合适的一个?

JavaEE应用服务器中的CompletableFuture / parallelStream

鉴于新的Java8,我们为异步任务获得了非常好的function,例如CompletableFuture和.paralellStream()。 如果你在Java SE中运行它,因为我已经理解它你将使用ForkJoinPool,但是如果我在例如Wildfly或TomcatEE中运行以下示例会发生什么? //Here I start a comp.Future without giving an Executor test = CompletableFuture.supplyAsync(() -> timeConsumingMethod()); //Here I start a parallel stream mList.paralell().filter(…).collect(Collectors.toList()) 会发生什么,我将从哪里借用我的资源 这些示例在@Stateful bean中运行 这些示例在@Stateless bean中运行 这些示例在CDI bean中运行

Java CompletableFuture的thenApply和thenApplyAsync有什么区别?

假设我有以下代码: CompletableFuture future = CompletableFuture.supplyAsync( () -> 0); thenApply案例: future.thenApply( x -> x + 1 ) .thenApply( x -> x + 1 ) .thenAccept( x -> System.out.println(x)); 这里的输出将是2.现在在thenApplyAsync情况下: future.thenApplyAsync( x -> x + 1 ) // first step .thenApplyAsync( x -> x + 1 ) // second step .thenAccept( x -> System.out.println(x)); // third step […]

如何取消Java 8可完成的未来?

我正在玩Java 8可完成的期货。 我有以下代码: CountDownLatch waitLatch = new CountDownLatch(1); CompletableFuture future = CompletableFuture.runAsync(() -> { try { System.out.println(“Wait”); waitLatch.await(); //cancel should interrupt System.out.println(“Done”); } catch (InterruptedException e) { System.out.println(“Interrupted”); throw new RuntimeException(e); } }); sleep(10); //give it some time to start (ugly, but works) future.cancel(true); System.out.println(“Cancel called”); assertTrue(future.isCancelled()); assertTrue(future.isDone()); sleep(100); //give it some time to finish […]

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

我有一些CompletableFuture ,我想并行运行它们,等待正常返回的第一个。 我知道我可以使用CompletableFuture.anyOf等待第一个返回,但这将正常或exception返回。 我想忽略exception。 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(); }

Java8 CompletableFuture recoverWith等价? 例如exception但返回CompletableFuture

我没有看到使用异步结果处理exception的明显方法。 例如,如果我想重试异步操作。 我希望这样的东西,但handleAsync不会做你认为它做的事情 – 它在异步运行另一个线程上的回调。 在这里返回CompletionStage是不正确的。 当天的危险问题:然后应用就像那时候的组合就是什么。 CompletionStage cf = askPong(“cause error”).handleAsync((x, t) -> { if (t != null) { return askPong(“Ping”); } else { return x; } }); askPong问演员的地方: public CompletionStage askPong(String message){ Future sFuture = ask(actorRef, message, 1000); final CompletionStage cs = toJava(sFuture); return cs; }

从CompletableFuture抛出exception

我有以下代码: // How to throw the ServerException? public void myFunc() throws ServerException{ // Some code CompletableFuture a = CompletableFuture.supplyAsync(() -> { try { return someObj.someFunc(); } catch(ServerException ex) { // throw ex; gives an error here. } })); // Some code } someFunc()抛出ServerException 。 我不想在这里处理它,但是将someFunc()的exceptionsomeFunc() myFunc()调用者。

为什么CompletableFuture.supplyAsync成功随机次数?

我是Java 8中lambdas和异步代码的新手。我不断得到一些奇怪的结果…… 我有以下代码: import java.util.concurrent.CompletableFuture; public class Program { public static void main(String[] args) { for (int i = 0; i < 100; i++) { String test = "Test_" + i; final int a = i; CompletableFuture cf = CompletableFuture.supplyAsync(() -> doPost(test)); cf.thenRun(() -> System.out.println(a)) ; } } private static boolean doPost(String t) { System.out.println(t); […]

使用CompletableFuture处理Java 8供应商exception

请考虑以下代码 – public class TestCompletableFuture { BiConsumer biConsumer = (x,y) -> { System.out.println(x); System.out.println(y); }; public static void main(String args[]) { TestCompletableFuture testF = new TestCompletableFuture(); testF.start(); } public void start() { Supplier numberSupplier = new Supplier() { @Override public Integer get() { return SupplyNumbers.sendNumbers(); } }; CompletableFuture testFuture = CompletableFuture.supplyAsync(numberSupplier).whenComplete(biConsumer); } } class SupplyNumbers […]

CompletableFuture的完成处理程序在哪个线程中执行?

我有一个关于CompletableFuture方法的问题: public CompletableFuture thenApply(Function fn) 事情是JavaDoc说的就是这样: 返回一个新的CompletionStage,当该阶段正常完成时,将使用此阶段的结果作为所提供函数的参数执行。 有关特殊完成的规则​​,请参阅CompletionStage文档。 线程怎么样? 这将在哪个线程中执行? 如果未来由线程池完成怎么办?