completable-future

Using values from previously chained thenCompose lambdas in Java 8

核能气质少年 提交于 2020-06-14 00:26:32
问题 The Java 8 coding style preferred by my colleagues is chaining asynchronous calls all the way through, e.g. CompletionStage<E> someMethod() { return doSomething().thenCompose(a -> { // ... return b; }).thenCompose(b -> { // ... return c; }).thenCompose(c -> { // ... return d; }).thenApply(d -> { // ... return e; }); } I have something like the above, but with an added challenge: I need to recall values retrieved within some of the lambdas, in later lambdas. For example, CompletionStage<E>

`Java 8 in Action` is wrong about the demo it provided?

僤鯓⒐⒋嵵緔 提交于 2020-06-11 05:59:07
问题 This code is a quoted from Java 8 in Action, which is also in the book 11.4.3. public Stream<CompletableFuture<String>> findPricesStream(String product) { return shops.stream() .map(shop -> CompletableFuture.supplyAsync(() -> shop.getPrice(product), executor)) .map(future -> future.thenApply(Quote::parse)) .map(future -> future.thenCompose(quote -> CompletableFuture.supplyAsync(() -> Discount.applyDiscount(quote), executor))); } Along the code, the writer enclose a figure as follows

Is there a converter from List<CompletionStage> to CompletionStage<List> in Java?

白昼怎懂夜的黑 提交于 2020-06-01 03:49:46
问题 Like in this hypothetical example of using of : List<CompletionStage<Long>> listOfFutureLongs = getFutureLongs(...) CompletionStage<List<Long>> futureListOfLongs = CompletionStage.of(listOfFutureLongs) 回答1: Strangely no. There's CompletableFuture.allOf for CompletableFuture , which is kind of like what you want, but no similar function for CompletionStage . You can either use CompletionStage.toCompletableFuture to get futures, or you can write your own. Unfortunately, the inability to check a

Why is CompletableFuture join/get faster in separate streams than using one stream

主宰稳场 提交于 2020-05-23 05:05:29
问题 For the following program I am trying to figure out why using 2 different streams parallelizes the task and using the same stream and calling join/get on the Completable future makes them take longer time equivalent to as if they were sequentially processed). public class HelloConcurrency { private static Integer sleepTask(int number) { System.out.println(String.format("Task with sleep time %d", number)); try { TimeUnit.SECONDS.sleep(number); } catch (InterruptedException e) { e

CompletableFuture chaining results

假如想象 提交于 2020-02-02 02:36:26
问题 I am trying to chain the calls/results of the methods to the next call. I get compile time error methodE because if am not able to get the reference of objB from the previous call. How can I pass the result of the previous call to the next chain? Have I completely misunderstood the process? Object objC = CompletableFuture.supplyAsync(() -> service.methodA(obj, width, height)) .thenApply(objA -> { try { return service.methodB(objA); } catch (Exception e) { throw new CompletionException(e); } }

CompletableFuture — Aggregate Future to Fail Fast

て烟熏妆下的殇ゞ 提交于 2020-02-01 04:49:07
问题 I have been using the CompletableFuture.allOf(...) helper to create aggregate futures that will only become "done" when their composite futures are marked as complete, i.e: CompletableFuture<?> future1 = new CompletableFuture<>(); CompletableFuture<?> future2 = new CompletableFuture<>(); CompletableFuture<?> future3 = new CompletableFuture<>(); CompletableFuture<?> future = CompletableFuture.allOf(future1, future2, future3); I would like a slight variation on this functionality, where the

How to ask CompletableFuture use non-daemon threads?

馋奶兔 提交于 2020-01-29 08:43:06
问题 I have wrote following code: System.out.println("Main thread:" + Thread.currentThread().getId()); CompletableFuture<Void> future = CompletableFuture.runAsync(() -> { try { System.out.println("Before sleep thread:" + Thread.currentThread().getId(), + " isDaemon:" + Thread.currentThread().isDaemon()); Thread.sleep(100); System.out.println("After sleep"); } catch (InterruptedException e) { e.printStackTrace(); } }); future.whenComplete((r, e) -> System.out.println("whenCompleted thread:" +

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

依然范特西╮ 提交于 2020-01-22 05:36:29
问题 To run some stuff in parallel or asynchronously I can use either an ExecutorService: <T> Future<T> submit(Runnable task, T result); or the CompletableFuture Api: static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor); (Lets assume I use in both cases the same Executor) Besides the return type Future vs. CompletableFuture are there any remarkable differences. Or When to use what? And what are the differences if I use the CompletableFuture API with default Executor

How to create a Mono from a completableFuture

被刻印的时光 ゝ 提交于 2020-01-15 12:18:07
问题 I am trying to wrap CompletableFuture inside a Reactor Mono type in order to simplify my transform operations. Project Reactor is more convenient in general! I am working inside an AWS Lambda function and I am invoking AWS services such as S3, SQS, etc... using the new AWS Java SDK 2.x version. This new SDK allows to make asynchronous calls to AWS services and returns CompleteableFuture objects. For example: S3AsyncClient s3AsyncClient = S3AsyncClient.builder().build(); Mono.fromFuture

How to create a Mono from a completableFuture

穿精又带淫゛_ 提交于 2020-01-15 12:18:05
问题 I am trying to wrap CompletableFuture inside a Reactor Mono type in order to simplify my transform operations. Project Reactor is more convenient in general! I am working inside an AWS Lambda function and I am invoking AWS services such as S3, SQS, etc... using the new AWS Java SDK 2.x version. This new SDK allows to make asynchronous calls to AWS services and returns CompleteableFuture objects. For example: S3AsyncClient s3AsyncClient = S3AsyncClient.builder().build(); Mono.fromFuture