Java Futures and Completable Futures Flashcards
https://hatchjs.com/future-vs-completablefuture-java-8/ https://www.callicoder.com/java-8-completablefuture-tutorial/
Completable Future - Simple Example
CompletableFuture<String> completableFuture = new CompletableFuture<String>(); This is the simplest CompletableFuture that you can have. All the clients who want to get the result of this CompletableFuture can call CompletableFuture.get() method - String result = completableFuture.get() The get() method blocks until the Future is complete. So, the above call will block forever because the Future is never completed. You can use CompletableFuture.complete() method to manually complete a Future - completableFuture.complete("Future's Result") All the clients waiting for this Future will get the specified result. And, Subsequent calls to completableFuture.complete() will be ignored.</String></String>
FutureTask
To create a Future
, you can use the FutureTask
class. The FutureTask
class implements the Future
interface, and it provides a number of methods for working with the task, such as get()
, cancel()
, and isDone()
. ExecutorService executor = Executors.newFixedThreadPool(1); FutureTask futureTask = new FutureTask<>(() -> { // Perform some long-running operation return “Hello world!”; }); executor.submit(futureTask); // Get the result of the task String result = futureTask.get();
Completable Future - Async Task
If you want to run some background task asynchronously and don’t want to return anything from the task, then you can use CompletableFuture.runAsync() method. It takes a Runnable object and returns CompletableFuture<Void>. // Run a task specified by a Runnable Object asynchronously. CompletableFuture<Void> future = CompletableFuture.runAsync(new Runnable() { @Override public void run() { // Simulate a long-running Job try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { throw new IllegalStateException(e); } System.out.println("I'll run in a separate thread than the main thread."); } }); // Block and wait for the future to complete future.get()</Void></Void>
Completable Future
A CompletableFuture
is a more powerful and flexible version of the Future
class. In addition to the methods that are available in Future
, CompletableFuture
also provides methods for: Completing the task with a value or an exception Running tasks in parallel Chaining tasks together Retrieving the results of tasks asynchronously
Future
A Future
is a representation of a task that is being executed in a separate thread. You can use a Future
to get the results of a task once it has finished, or you can cancel the task if you need to
Completable Future - Return Result - supplyAsync()
// Run a task specified by a Supplier object asynchronously CompletableFuture<String> future = CompletableFuture.supplyAsync(new Supplier<String>() { @Override public String get() { try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { throw new IllegalStateException(e); } return "Result of the asynchronous computation"; } }); // Block and get the result of the Future String result = future.get(); System.out.println(result);</String></String>