Java Completable Futures and Threads Flashcards

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

1
Q

Completable Future

A

CompletableFuture is a class introduced in Java 8 that allows us to write asynchronous, non-blocking code

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

Completable Future Example

A

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "Hello, world!";
});</String>

future.thenAccept(result -> System.out.println(result));

We’re then calling the thenAccept() method on the CompletableFuture object to specify what to do when the operation is complete.

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

Chaining async operations

A

CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 10)
.thenApplyAsync(result -> result * 2)
.thenApplyAsync(result -> result + 5);</Integer>

future.thenAccept(result -> System.out.println(result));

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

Completable Future - Blocking vs Non Blocking

A

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> longRunningCall());
future.thenAccept(result -> System.out.println("Got: " + result));
The main thread does no wait. When the long running task completes the call back of thenAccept is loaded onto the call stack
But if you do future.get(); The main thread blocks till the asyn code returns the value.</String>

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

Handling Exception - with exceptionally

A

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
if (true) throw new RuntimeException("Boom!");
return "Hello";
}).exceptionally(ex -> {
System.out.println("Caught exception: " + ex.getMessage());
return "Fallback value";
});</String>

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

Handling Exceptions - with handle

A

handle() is more powerful — it gives you both the result and the exception, and lets you return a new value either way.

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
throw new RuntimeException("Oops");
}).handle((result, ex) -> {
if (ex != null) {
System.out.println("Handled exception: " + ex.getMessage());
return "Recovered";
}
return result;
});</String>

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

Tip - Exception Handling

A

Always end chains with an exception handler

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

runAsync with executor

A

ExecutorService executor = Executors.newFixedThreadPool(10);

CompletableFuture.runAsync(() -> {
// Your logic here
}, executor);

Without executor, ForkJoinPool.commonPool() default exectutor is used
Limited threads → usually availableProcessors() - 1
Shared across the JVM

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