completable-future

Proper way to adapt an exception with CompletableFuture

霸气de小男生 提交于 2020-01-15 10:09:57
问题 I am working on chaining CompletableFuture to adapt an exception. While I have something that is working, I don't understand why it works. @Test public void futureExceptionAdapt() throws ExecutionException, InterruptedException { class SillyException extends Exception { } class AdaptedException extends Exception { AdaptedException(SillyException silly) { } } CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> { sleepForThreeSeconds(); if (true) throw new

How to divide 1 completablefuture to many completablefuture in stream?

喜你入骨 提交于 2020-01-13 10:09:24
问题 For example I have such methods: public CompletableFuture<Page> getPage(int i) { ... } public CompletableFuture<Document> getDocument(int i) { ... } public CompletableFuture<Void> parseLinks(Document doc) { ... } And my flow: List<CompletableFuture> list = IntStream .range(0, 10) .mapToObj(i -> getPage(i)) // I want method like this: .thenApplyAndSplit(CompletableFuture<Page> page -> { List<CompletableFuture<Document>> docs = page.getDocsId() .stream() .map(i -> getDocument(i)) .collect

Why is `parallelStream` faster than the `CompletableFuture` implementation?

倾然丶 夕夏残阳落幕 提交于 2020-01-12 18:48:32
问题 I wanted to increase the performance of my backend REST API on a certain operation that polled multiple different external APIs sequentially and collected their responses and flattened them all into a single list of responses. Having just recently learned about CompletableFuture s, I decided to give it a go, and compare that solution with the one that involved simply changing my stream for a parallelStream . Here is the code used for the benchmark-test: package com.alithya.platon; import java

Create CompletableFuture from a sync method call

社会主义新天地 提交于 2020-01-05 07:10:15
问题 I would like to know if a one-liner exists for creating a CompletableFuture from a synchron method call. If no, why? Long version: final CompletableFuture<ReturnType> future = new CompletableFuture<>(); final String parameters = "hello"; ReturnType result; try { result = syncMethodCall(parameters); } catch (Exception e) { future.completeExceptionally(e); } future.complete(result); return future; Short desired version (or kind): final String parameters = "hello"; return CompletableFuture

How to use CompletableFuture without risking a StackOverflowError?

倾然丶 夕夏残阳落幕 提交于 2020-01-04 06:28:45
问题 I want to walk the search space of an asynchronous function. I coded the logic as follows: /** * Assuming that a function maps a range of inputs to the same output value, minimizes the input value while * maintaining the output value. * * @param previousInput the last input known to return {@code target} * @param currentInput the new input value to evaluate * @param function maps an input to an output value * @param target the expected output value * @return the minimum input value that

How to use CompletableFuture without risking a StackOverflowError?

佐手、 提交于 2020-01-04 06:28:29
问题 I want to walk the search space of an asynchronous function. I coded the logic as follows: /** * Assuming that a function maps a range of inputs to the same output value, minimizes the input value while * maintaining the output value. * * @param previousInput the last input known to return {@code target} * @param currentInput the new input value to evaluate * @param function maps an input to an output value * @param target the expected output value * @return the minimum input value that

Listenablefuture vs Completablefuture

删除回忆录丶 提交于 2019-12-31 08:08:09
问题 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. 回答1: 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:

Listenablefuture vs Completablefuture

馋奶兔 提交于 2019-12-31 08:08:01
问题 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. 回答1: 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:

What is the correct way to create an already-completed CompletableFuture<Void>

允我心安 提交于 2019-12-30 07:59:10
问题 I am using Completable futures in java 8 and I want to write a method that, based on a received parameter, either runs multiple tasks with side effects in parallel and then return their "combined" future (using CompletableFuture.allOf() ), or does nothing and returns an already-completed future. However, allOf returns a CompletableFuture<Void> : public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs) And the only way to create an already-completed future that know is using

How to fail a java.util.concurrent.Future

坚强是说给别人听的谎言 提交于 2019-12-24 17:08:31
问题 There is pretty heavy use of io.vertx.core.Future in the vertx ecosystem: https://vertx.io/docs/apidocs/io/vertx/core/Future.html An example of using Vertx Future is here: private Future<Void> prepareDatabase() { Future<Void> future = Future.future(); dbClient = JDBCClient.createShared(vertx, new JsonObject(...)); dbClient.getConnection(ar -> { if (ar.failed()) { LOGGER.error("Could not open a database connection", ar.cause()); future.fail(ar.cause()); // here return; } SQLConnection