在 Java 编程中,异步编程是一种重要的技术,它允许你在执行长时间运行的任务时不会阻塞主线程。为了支持异步编程,Java 提供了 Future 和 CompletableFuture 这两个关键的类。在本文中,我们将比较它们的特点、优缺点以及使用场景。
特点:
Future 是 Java 5 引入的接口,用于表示一个异步计算的结果。Future 可以通过 get 方法来获取异步操作的结果,但该方法是阻塞的,如果异步操作未完成,它会一直等待。Future 不提供一种直接的方式来添加回调函数,处理操作完成后的结果或异常。Future 只能表示异步任务是否完成,而不能手动触发任务的完成或组合多个任务。优点:
缺点:
get 方法会阻塞当前线程,如果异步任务很慢或永远不完成,会导致程序出现长时间阻塞。使用场景:
特点:
CompletableFuture 是 Java 8 引入的类,是 Future 的增强版。thenApply、thenCompose)来处理操作完成后的结果。优点:
缺点:
使用场景:
现在让我们深入了解一下 CompletableFuture 的使用方法。
你可以使用不同的方式创建 CompletableFuture 对象,最常见的是使用 CompletableFuture.supplyAsync 或 CompletableFuture.runAsync 方法,这些方法接受一个 Supplier 或 Runnable 作为参数,并返回一个 CompletableFuture 对象。
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 42);
CompletableFuture<Void> futureVoid = CompletableFuture.runAsync(() -> doSomething());
CompletableFuture 允许你执行异步操作,这些操作不会阻塞当前线程。你可以将需要执行的操作包装在 CompletableFuture 中,然后使用 thenApply、thenCompose、thenAccept 等方法来定义操作完成后的回调。
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 42);
CompletableFuture<String> futureResult = future.thenApply(result -> "Result: " + result);
你可以将多个 CompletableFuture 组合在一起,以便在它们都完成时执行某个操作,或者在其中任何一个完成时执行某个操作。这可以使用 thenCombine、thenCompose、thenCombineAsync 等方法来实现。
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> 42);
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> 23);
CompletableFuture<Integer> combinedFuture = future1.thenCombine(future2, (result1, result2) -> result1 + result2);
CompletableFuture 允许你处理异步操作中可能发生的异常。你可以使用 exceptionally 或 handle 方法来捕获和处理异常。
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
// Simulate an exception
throw new RuntimeException("Error!");
});
CompletableFuture<Integer> resultFuture = future.exceptionally(ex -> {
System.out.println("Caught exception: " + ex.getMessage());
return 0; // Provide a default value
});
你可以使用 get 方法等待 CompletableFuture 的完成,但要小心,因为它会阻塞当前线程,直到任务完成。
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 42);
try {
Integer result = future.get();
System.out.println("Result: " + result);
} catch (Exception e) {
// Handle exceptions
}
使用 CompletableFuture.allOf 或 CompletableFuture.anyOf 可以组合多个 CompletableFuture,以等待它们全部完成或任何一个完成。
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> 42);
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> 23);
CompletableFuture<Void> allOfFuture = CompletableFuture.allOf(future1, future2);
CompletableFuture<Object> anyOfFuture = CompletableFuture.anyOf(future1, future