Skip to content
Advertisement

Tag: completable-future

How to return a value from a nested CompletableFuture without blocking?

I’m having trouble returning car price value using getPrice() method because of the following error: I want getPrice return CompletableFuture<Double> but instead it returns CompletableFuture<CompletableFuture<Double>> because I’m returning a value from a nested future. I could call .join() on the nested future but I don’t want to block the thread. This is my code: Answer The operation you’re looking for

Java concurrent programming – endless loop

I am reading the book Introducing Play Framework: Java Web Application Development (ISBN 978-1-4842-5645-9) and there is this example on Callable: My question is, if the computation of the Future throws and exception, will the while loop run forever? In my opinion, yes, it will loop forever. First, if the Future computation throws an exception, then future.isDone() always evaluates to

How to call a method and run it asynchronously?

Is there a way to run getUsers() asynchronously only from main() in Java 8? Running getUsers() on the main thread would take 300 seconds. I wish to make it in less than 180 seconds with 4 cores. Without modifying getUsers() (the objective), running the following is still taking 300 seconds: Answer You can divide your loop of users creation into

Why does calling CompletableFuture::cancel cause an immediate CancellationException

I am trying to call cancel on CompletableFuture. It seems from the docs: If not already completed, completes this CompletableFuture with a CancellationException. Dependent CompletableFutures that have not already completed will also complete exceptionally, with a CompletionException caused by this CancellationException. That it should complete them exceptionally which is what I was expecting but instead, it throws and immediate CancellationException.

UserTransaction jndi lookup failed when using CompletableFuture

I have a code which does context lookup to get UserTransaction JNDI as ctx.lookup(“java:comp/UserTransaction”). When I run this code without using CompletableFuture, it works as expected. When working with CompletableFuture in async thread, it gives exception saying jndi lookup failed. I tried to check if I can get the required JNDI from global scope, but no luck. Answer The problem

Asynchronous execution/operation with CompletableFuture in Java 8+

In Java, is calling get() method while looping on CompletableFuture instances as good as doing synchronous operations although CompletableFuture is used for async calls? Answer ‘get()’ waits until the future is completed. If that’s what you want, it’s what you use. There’s no general rule. For example, you might be using a method that is inherently asynchronous, but in your

Exception propagation in CompletableFuture (java)

How can I propagate exception encountered in the following code inside CompletableFuture.runAsync to my main thread? I want to catch the IllegalStateException in my main thread. Answer One option would be to create a Collection of Throwable objects and when the CompletableFuture completes you can add the exception to the Collection (if it’s not null). Then on your main thread

Advertisement