Skip to main content

Posts

Showing posts with the label Java Async

Completable Future - Java 8 Async

We know that java 5 had Future interface for representing the result of asynchronous executions in a multithreaded environment. Java 8 introduces CompletableFuture which is very advanced and has overcome some issues that were there in Future - Java 5. 1. It didn't have exception handling capability - lets say there was exception thrown by the thread executing asynchronously - then that exception is lost, it will not be propagated to main thread. 2. Call back function capability was not there If you want a function to be called when the async thread completes and Future result was available, it was not possible. Only way was to call get() method and block for it's completion. 3. Chaining multiple callbacks or Futures was not possible If you have several asynchronous calculations to be chained together it was not possible. Again only was to use get() method and blocking on it. Completable Future Lets see how to create a Completable Future CompletableFuture<String> future=n