completable-future

What is the order in which multiple thenAccept blocks of a CompletableFuture are executed

我与影子孤独终老i 提交于 2019-12-22 04:37:48
问题 So I have a method that returns a CompletableFuture . Before returning, this method adds a block with thenAccept which is executed after the CompletableFuture completes. The caller of this method also adds another block with thenAccept . Obviously this can go on with multiple chained calls. In what order are the CompletionStage returned by the thenAccept invocations executed? Is it guaranteed to be the order in which they are added? If not, how can one guarantee that they are executed in the

CompletableFuture | thenApplyAsync vs thenCompose and their use cases [duplicate]

老子叫甜甜 提交于 2019-12-21 21:30:53
问题 This question already has answers here : CompletableFuture | thenApply vs thenCompose (4 answers) Closed 2 years ago . I was trying to understand CompletableFuture, and came across 2 methods, thenApplyAsync and thenCompose. Am trying to understand the difference between these two. CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> { System.out.println(Thread.currentThread().getName() + " Printing hello"); return "Hello"; }).thenCompose((String s) -> { return

What is the difference between 'CompletionStage' and 'CompletableFuture'

别说谁变了你拦得住时间么 提交于 2019-12-21 03:17:34
问题 I have seen an example in each of them, but I need to know exactly what is the difference in deep, Because sometimes I think I can use both of them to get the same result, So I want know so that I can choose the correct one? what is the benefit of using each of them? Like this example both are worked: public CompletionStage<Result> getNextQueryUUID() { return CompletableFuture.supplyAsync(() -> { String nextId = dbRequestService.getNextRequestQueryUUID(); return ok(nextId); }, executor); }

Does CompletableFuture have a corresponding Local context?

半世苍凉 提交于 2019-12-21 01:22:17
问题 In the olden days, we had ThreadLocal for programs to carry data along with the request path since all request processing was done on that thread and stuff like Logback used this with MDC.put("requestId", getNewRequestId()); Then Scala and functional programming came along and Future s came along and with them came Local.scala (at least I know the twitter Future s have this class). Future.scala knows about Local.scala and transfers the context through all the map / flatMap , etc. etc.

Does CompletableFuture have a corresponding Local context?

夙愿已清 提交于 2019-12-21 01:21:03
问题 In the olden days, we had ThreadLocal for programs to carry data along with the request path since all request processing was done on that thread and stuff like Logback used this with MDC.put("requestId", getNewRequestId()); Then Scala and functional programming came along and Future s came along and with them came Local.scala (at least I know the twitter Future s have this class). Future.scala knows about Local.scala and transfers the context through all the map / flatMap , etc. etc.

How do I get a CompletableFuture<T> from an Async Http Client request?

眉间皱痕 提交于 2019-12-20 20:41:14
问题 On Async Http Client documentation I see how to get a Future<Response> as the result of an asynchronous HTTP Get request simply doing, for example: AsyncHttpClient asyncHttpClient = new DefaultAsyncHttpClient(); Future<Response> f = asyncHttpClient .prepareGet("http://api.football-data.org/v1/soccerseasons/398") .execute(); Response r = f.get(); However, for convenience I would like to get a CompletableFuture<T> instead, for which I could apply a continuation that converts the result in

How to chose an Executor for CompletableFuture::supplyAsync

狂风中的少年 提交于 2019-12-20 12:04:05
问题 CompletableFuture::supplyAsync(() -> IO bound queries) How do I chose an Executor for CompletableFuture::supplyAsync to avoid polluting the ForkJoinPool.commonPool() . There are many options in Executors ( newCachedThreadPool , newWorkStealingPool , newFixedThreadPool etc) And I read about new ForkJoinPool here How do I chose the right one for my use case ? 回答1: You should use public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor) method. As executor you

Mono vs CompletableFuture

孤人 提交于 2019-12-20 10:37:13
问题 CompletableFuture executes a task on a separate thread ( uses a thread-pool ) and provides a callback function. Let's say I have an API call in a CompletableFuture . Is that an API call blocking? Would the thread be blocked till it does not get a response from the API? ( I know main thread/tomcat thread will be non-blocking, but what about the thread on which CompletableFuture task is executing? ) Mono is completely non-blocking, as far as I know. Please shed some light on this and correct me

Does completableFuture in Java 8 scale to multiple cores?

旧时模样 提交于 2019-12-19 09:48:10
问题 Lets say I have a single thread that calls bunch of methods that return completablefuture and say I add all of them to a list and in the end I do completablefutures.allof(list_size).join() . Now does the futures in the list can scale to multiple cores? other words are the futures scheduled into multiple cores to take advantage of parallelism? 回答1: CompletableFuture represents a task which is associated with some Executor. If you did not specify executor explicitly (for example, you used

How do you access completed futures passed to CompletableFuture allOf?

痴心易碎 提交于 2019-12-19 02:18:22
问题 I am trying to get a grip of Java 8 CompletableFuture. How can I join these to person and return them after "allOf". The code under is not working but gives you an idea of what I have tried. In javascript ES6 i would do Promise.all([p1, p2]).then(function(persons) { console.log(persons[0]); // p1 return value console.log(persons[1]); // p2 return value }); My efforts in Java so far public class Person { private final String name; public Person(String name) { this.name = name; } public String