completable-future

In which thread does CompletableFuture's completion handlers execute in?

夙愿已清 提交于 2019-11-27 04:30:59
I have a question about CompletableFuture method: public <U> CompletableFuture<U> thenApply(Function<? super T, ? extends U> fn) The thing is the JavaDoc says just this: Returns a new CompletionStage that, when this stage completes normally, is executed with this stage's result as the argument to the supplied function. See the CompletionStage documentation for rules covering exceptional completion. What about threading? In which thread this is going to be executed? What if the future is completed by a thread pool? The policies as specified in the CompletableFuture docs could help you

Throwing exception from CompletableFuture

人盡茶涼 提交于 2019-11-27 03:58:45
I have the following code: // How to throw the ServerException? public void myFunc() throws ServerException{ // Some code CompletableFuture<A> a = CompletableFuture.supplyAsync(() -> { try { return someObj.someFunc(); } catch(ServerException ex) { // throw ex; gives an error here. } })); // Some code } someFunc() throws a ServerException . I don't want to handle this here but throw the exception from someFunc() to caller of myFunc() . Your code suggests that you are using the result of the asynchronous operation later in the same method, so you’ll have to deal with CompletionException anyway,

How to cancel Java 8 completable future?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 03:56:49
问题 I am playing with Java 8 completable futures. I have the following code: 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

Surprising behavior of Java 8 CompletableFuture exceptionally method

不想你离开。 提交于 2019-11-27 02:03:26
问题 I have encountered strange behavior of Java 8 CompletableFuture.exceptionally method. If I execute this code, it works fine and prints java.lang.RuntimeException CompletableFuture<String> future = new CompletableFuture<>(); future.completeExceptionally(new RuntimeException()); future.exceptionally(e -> { System.out.println(e.getClass()); return null; }); But if I add another step in the future processing like thenApply , the exception type changes to java.util.concurrent.CompletionException

List<Future> to Future<List> sequence

最后都变了- 提交于 2019-11-26 18:24:48
I am trying to convert List<CompletableFuture<X>> to CompletableFuture<List<T>> . This is quite useful as when you have many asynchronous tasks and you need to get results of all of them. If any of them fails then the final future fails. This is how I have implemented: public static <T> CompletableFuture<List<T>> sequence2(List<CompletableFuture<T>> com, ExecutorService exec) { if(com.isEmpty()){ throw new IllegalArgumentException(); } Stream<? extends CompletableFuture<T>> stream = com.stream(); CompletableFuture<List<T>> init = CompletableFuture.completedFuture(new ArrayList<T>()); return

CompletableFuture already completed with an exception

五迷三道 提交于 2019-11-26 17:49:01
问题 CompletableFuture.completedFuture() returns a CompletedFuture that is already completed with the given value. How do we construct a CompletableFuture that is already completed exceptionally? Meaning, instead of returning a value I want the future to throw an exception. 回答1: Unlike Java 9 and later, Java 8 does not provide a static factory method for this scenario. The default constructor can be used instead: CompletableFuture<T> future = new CompletableFuture<>(); future.completeExceptionally

Why is CompletableFuture.supplyAsync succeeding a random number of times?

谁都会走 提交于 2019-11-26 17:21:48
问题 I'm new to both lambdas and asynchronous code in Java 8. I keep getting some weird results... I have the following code: 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<Boolean> cf = CompletableFuture.supplyAsync(() -> doPost(test)); cf.thenRun(() -> System.out.println(a)) ; } } private static boolean doPost(String t) { System.out.println

Java 8 Supplier Exception handling with CompletableFuture

假装没事ソ 提交于 2019-11-26 16:55:38
问题 Consider the following code public class TestCompletableFuture { BiConsumer<Integer, Throwable> 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<Integer> numberSupplier = new Supplier<Integer>() { @Override public Integer get() { return SupplyNumbers.sendNumbers(); } }; CompletableFuture<Integer> testFuture =

Throwing exception from CompletableFuture

我只是一个虾纸丫 提交于 2019-11-26 15:54:40
问题 I have the following code: // How to throw the ServerException? public void myFunc() throws ServerException{ // Some code CompletableFuture<A> a = CompletableFuture.supplyAsync(() -> { try { return someObj.someFunc(); } catch(ServerException ex) { // throw ex; gives an error here. } })); // Some code } someFunc() throws a ServerException . I don't want to handle this here but throw the exception from someFunc() to caller of myFunc() . 回答1: Your code suggests that you are using the result of

In which thread does CompletableFuture&#39;s completion handlers execute in?

旧城冷巷雨未停 提交于 2019-11-26 08:23:14
问题 I have a question about CompletableFuture method: public <U> CompletableFuture<U> thenApply(Function<? super T, ? extends U> fn) The thing is the JavaDoc says just this: Returns a new CompletionStage that, when this stage completes normally, is executed with this stage\'s result as the argument to the supplied function. See the CompletionStage documentation for rules covering exceptional completion. What about threading? In which thread this is going to be executed? What if the future is