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 ca…
Tag: completable-future
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 Fut…
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 div…
Are CompletableFutures thread safe? [closed]
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 1 year ago. Improve this question I have a thread that invokes to separate threads. It passes in the same CompletableFuture to …
stop other service calls if using Completable future to call multiple services if any of service fails
I am using completable future to invoke multiple service calls. And then I am using : Well why am i using this kind of code ??? by doing that I am able to improve performance. If service1 succeeds with 200 & takes 3 sec of time , service2 succeeds with 200 & takes 3 sec of time , service3 succeeds
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 Cancellatio…
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 …
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 gen…
Is there a way to calculate the time required to complete every completableFuture?
Before I jump into the specifics of the question, I want to give the context of the problem. Basically, my code looks like Now what i want to do is run doSomething() as a completableFuture and get the responseTime as mentioned above. But since doing something like this would be totally incorrect — since…
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 exce…