Java Futures and Completable Futures Flashcards

https://hatchjs.com/future-vs-completablefuture-java-8/ https://www.callicoder.com/java-8-completablefuture-tutorial/

1
Q

Completable Future - Simple Example

A

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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

FutureTask

A

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();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Completable Future - Async Task

A

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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Completable Future

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Future

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Completable Future - Return Result - supplyAsync()

A

// 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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly