CompletableFuture
CompletableFuture
is a class in Java that allows you to work with asynchronous operations and promises, making it easier to write concurrent and non-blocking code. You can use CompletableFuture
to represent a future result of an asynchronous computation and perform various operations on the result. Here are some common usage patterns with code examples:
1. Creating a CompletableFuture:
You can create a CompletableFuture
and provide a computation that will be executed asynchronously. You can use supplyAsync
to run a function and return a result:
import java.util.concurrent.CompletableFuture;
public class CompletableFutureExample {
public static void main(String[] args) {
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 42);
}
}
2. Chaining Operations:
You can chain operations on a CompletableFuture
using methods like thenApply
, thenCompose
, and thenAccept
:
import java.util.concurrent.CompletableFuture;
public class CompletableFutureExample {
public static void main(String[] args) {
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 21)
.thenApply(result -> result * 2)
.thenAccept(result -> System.out.println("Final result: " + result));
}
}
3. Handling Errors:
You can handle exceptions with the exceptionally
method:
import java.util.concurrent.CompletableFuture;
public class CompletableFutureExample {
public static void main(String[] args) {
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 10 / 0)
.exceptionally(ex -> {
System.err.println("An error occurred: " + ex.getMessage());
return 0;
});
}
}
4. Combining Multiple CompletableFutures:
You can combine the results of multiple CompletableFuture
instances using methods like thenCombine
and thenCompose
:
import java.util.concurrent.CompletableFuture;
public class CompletableFutureExample {
public static void main(String[] args) {
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> 21);
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> 21);
CompletableFuture<Integer> combined = future1.thenCombine(future2, (result1, result2) -> result1 + result2);
}
}
5. Waiting for Completion:
You can block and wait for a CompletableFuture
to complete using the get
method. Be cautious when using get
, as it can block the current thread:
import java.util.concurrent.CompletableFuture;
public class CompletableFutureExample {
public static void main(String[] args) {
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 42);
try {
Integer result = future.get(); // This blocks until the result is available
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
6. Combining Multiple Futures:
You can wait for multiple CompletableFuture
instances to complete using CompletableFuture.allOf
:
import java.util.concurrent.CompletableFuture;
public class CompletableFutureExample {
public static void main(String[] args) {
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> 21);
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> 42);
CompletableFuture<Void> combinedFuture = CompletableFuture.allOf(future1, future2);
try {
combinedFuture.get(); // This blocks until both futures are completed
} catch (Exception e) {
e.printStackTrace();
}
}
}
CompletableFuture
is a powerful tool for working with asynchronous tasks and is commonly used in Java applications to improve concurrency and responsiveness. It provides a flexible and composable way to work with asynchronous operations.
Leave a Comment