Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 Completable Future

My problem is how to use Completable Future.

I have a class that implements Callable.

public class Executor implements Callable<Collection>

Earlier is used to do -

service.submit(collectorService);

Which would return a Future<Collection>. However we don't want to use future anymore and need CompletableFuture . One idea is that we don't need to poll with CompletableFuture and We don't have to wait and block until it's ready.

So how would i use completable future and call a function say isDone() when the callable thread finishes.

like image 909
ayush Avatar asked Mar 09 '15 13:03

ayush


1 Answers

Given a CompletableFuture<T> f, you can kick off a synchronous or asynchronous task to run upon completion using:

f.thenApply(result -> isDone(result));      // sync callback
f.thenApplyAsync(result -> isDone(result)); // async callback

...or, if you don't need the result:

f.thenRun(() -> isDone());
f.thenRunAsync(() -> isDone());
like image 108
Mike Strobel Avatar answered Oct 17 '22 18:10

Mike Strobel