FAQ Database Discussion Community


How to interrupt underlying execution of CompletableFuture

java,concurrency,completable-future
I know that completablefutures design does not control its execution with interruptioons, but i suppose some of you might have this problem. CompletableFutures are very good way to compose async execution, but given the case when you want the underlying execution to be interrupted or stopped when future is cancled,...

How to close AsyncHttpClient with Netty for an asynchronous Http request?

java,asynchronous,netty,asynchttpclient,completable-future
Using the AsyncHttpClient with Netty provider will prevent the main program to terminate when we execute an asynchronous request. For instance, the following program terminates after the println, or not, depending on whether the provider is JDKAsyncHttpProvider or NettyAsyncHttpProvider: public class Program { public static CompletableFuture<Response> getDataAsync(String uri) { final...

When is CompletableFuture actually completed?

java,completable-future
Here's the MCVE: public static void main(String[] args) { CompletableFuture<String> r1 = CompletableFuture.supplyAsync(() -> { try { Thread.sleep(5000); } catch (InterruptedException e) { throw new RuntimeException(e); } return "41"; }); CompletableFuture<String> r2 = CompletableFuture.supplyAsync(() -> "42"); CompletableFuture<String> r3 = CompletableFuture.supplyAsync(() -> { System.out.println("I'm called."); return "43"; }); CompletableFuture.allOf(r1, r2, r3).thenRun(()...

What is the recommended way to wait till the Completable future threads finish

java,multithreading,concurrency,completable-future
I am using CompletableFuture as shown below in the code. but concerning the way i should wait till all runnables finish, i found two ways and i do not know the difference between them and which one is the best practice? they are as follows: code: this.growSeedFutureList = CompletableFuture.runAsync(new GrowSeedSERun(this.saliencyMat,...

Best way to sequentially pass list values to single value consumer?

java,java-8,producer-consumer,completable-future
I'm toying with Java8's streams and CompletableFutures. My pre-existing code has a class that takes a single URL and downloads it: public class FileDownloader implements Runnable { private URL target; public FileDownloader(String target) { this.target = new URL(target); } public void run() { /* do it */ } } Now,...

How to avoid concurrent access to a resource?

java,multithreading,synchronization,completable-future
I have four threads runs asynchronously using CompletableFuture as shown below in the code. and all of them should access "grownSeedXYList". sometime when i run the code i receive no errors, but some time i receive "java.util.concurrent.completionexception" and i thinkk this is because the "grownSeedXYList" is not synchronized. please let...