completable-future

Java 8 CompletableFuture.allOf(…) with Collection or List [duplicate]

喜夏-厌秋 提交于 2019-12-02 17:25:01
This question already has an answer here: List<Future> to Future<List> sequence 8 answers Java 8 has a function CompletableFuture.allOf(CompletableFuture<?>...cfs) that returns a CompletableFuture that is completed when all the given futures complete. However, I almost always am not dealing with an array of CompletableFuture s, but instead have a List<CompletableFuture> . Of course, I can use the toArray() method, but this ends up being a bit of a pain to have to constantly convert back and forth between arrays and lists. It would be really nice if there were a slick way get a

Listenablefuture vs Completablefuture

馋奶兔 提交于 2019-12-02 15:24:45
I tried hard but didn't find any article or blog which clearly compares ListenableFuture and CompletableFuture , and provides a good analysis. So if anyone can explain or point me to such a blog or article, it will be really good for me. Livia Moroianu Both ListenableFuture and CompletableFuture have an advantage over its parent class Future by allowing the caller to "register" in one way or another a callback to be called when the async action has been completed. With Future you can do this: ExecutorService executor = ...; Future f = executor.submit(...); f.get(); f.get() gets blocked until

Nested Futures not executing

瘦欲@ 提交于 2019-12-02 00:05:34
问题 I encountered a strange situation. I'm fiddling with CompletableFuture and when running the following code I have unexpected results: public static void main(String[] args) { CompletableFuture<CompletableFuture<CompletableFuture<CompletableFuture<CompletableFuture<CompletableFuture<Object>>>>>> completableFutureCompletableFuture = CompletableFuture.supplyAsync(() -> { System.out.println("first"); return CompletableFuture.supplyAsync(() -> { System.out.println("second"); return

Nested Futures not executing

こ雲淡風輕ζ 提交于 2019-12-01 22:40:09
I encountered a strange situation. I'm fiddling with CompletableFuture and when running the following code I have unexpected results: public static void main(String[] args) { CompletableFuture<CompletableFuture<CompletableFuture<CompletableFuture<CompletableFuture<CompletableFuture<Object>>>>>> completableFutureCompletableFuture = CompletableFuture.supplyAsync(() -> { System.out.println("first"); return CompletableFuture.supplyAsync(() -> { System.out.println("second"); return CompletableFuture.supplyAsync(() -> { System.out.println("third"); return CompletableFuture.supplyAsync(() -> { System

How to kill CompletableFuture related threads?

*爱你&永不变心* 提交于 2019-12-01 12:32:52
问题 I have method that is checking the CompletableFuture execution time. If such CompletableFuture is executing for more than 2 seconds i want to kill this task. But how can I doit if i don't have control overy thread where CompletableFuture methods are executed ? final CompletableFuture<List<List<Student>>> responseFuture = new CompletableFuture<>(); responseFuture.supplyAsync(this::createAllRandomGroups) .thenAccept(this::printGroups) .exceptionally(throwable -> { throwable.printStackTrace();

Map exception in completable future to a different exception type?

柔情痞子 提交于 2019-12-01 11:38:44
I'm using java 8's completable futures and I'd like to be able take an exception that is throwing by the future and transform it to a different exception. All the composite stuff I've tried seems to get short circuited once an exception occurs. Using a scala future, for example, I can do something like this: scala.concurrent.Future<Object> translatedException = ask.recover(new Recover<Object>() { @Override public Object recover(final Throwable failure) throws Throwable { if (failure instanceof AskTimeoutException) { throw new ApiException(failure); } throw failure; } }, actorSystem.dispatcher(

Map exception in completable future to a different exception type?

扶醉桌前 提交于 2019-12-01 09:24:13
问题 I'm using java 8's completable futures and I'd like to be able take an exception that is throwing by the future and transform it to a different exception. All the composite stuff I've tried seems to get short circuited once an exception occurs. Using a scala future, for example, I can do something like this: scala.concurrent.Future<Object> translatedException = ask.recover(new Recover<Object>() { @Override public Object recover(final Throwable failure) throws Throwable { if (failure

How do you access completed futures passed to CompletableFuture allOf?

你说的曾经没有我的故事 提交于 2019-11-30 20:14:59
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 getName() { return name; } } @Test public void combinePersons() throws ExecutionException,

CompletableFuture/parallelStream in JavaEE app server

不打扰是莪最后的温柔 提交于 2019-11-30 19:18:13
Given the new Java8 we are getting really nice features for asynchronous tasks, e.g. CompletableFuture and .paralellStream(). If you run this in Java SE as I've understood it you will utilize the ForkJoinPool, but what happens if I run the following examples in e.g. Wildfly or 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()) What happens and where will I borrow my resources from if The examples are ran in a @Stateful bean

How to create async stacktraces?

心已入冬 提交于 2019-11-30 13:34:23
UPDATE : The latest version of Intellij IDEA implements exactly what I'm looking for. The question is how to implement this outside of the IDE (so I can to dump async stack traces to log files), ideally without the use of an instrumenting agent. Ever since I converted my application from a synchronous to asynchronous model I am having problems debugging failures. When I use synchronous APIs, I always find my classes in exception stacktraces so I know where to begin looking if something goes wrong. With asynchronous APIs, I am getting stacktraces that do not reference my classes nor indicate