CompletableFuture用法

CompletableFuture 是 Java 8 引入的一个类,位于 java.util.concurrent 包中,它是 Java 对异步编程的一种支持。CompletableFuture 提供了丰富的 API,可以帮助你以非阻塞的方式异步执行任务,并且能够很方便地进行任务的组合和处理。

基本概念

  • 异步编程:允许你在不阻塞主线程的情况下执行任务。当任务完成时,你可以通过回调函数来处理结果。
  • CompletableFuture:一个可完成的 Future,它不仅可以代表一个异步计算的结果,还可以手动完成这个计算。

创建 CompletableFuture

你可以通过以下几种方式创建一个 CompletableFuture

  1. 直接创建一个未完成的 CompletableFuture

    CompletableFuture<String> future = new CompletableFuture<>();
    
  2. 使用静态方法 supplyAsync 创建并运行异步任务

    CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello, World!");
    
  3. 使用静态方法 runAsync 创建并运行异步任务(不返回结果)

    CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
        // Do some task
    });
    

异步任务的组合

CompletableFuture 提供了多种方法来组合多个异步任务:

  • thenApply:在 CompletableFuture 完成后应用一个函数。

    CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello")
        .thenApply(result -> result + ", World!");
    
  • thenAccept:在 CompletableFuture 完成后消费结果。

    CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> "Hello")
        .thenAccept(result -> System.out.println(result));
    
  • thenCompose:在 CompletableFuture 完成后应用一个返回 CompletableFuture 的函数。

    CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello")
        .thenCompose(result -> CompletableFuture.supplyAsync(() -> result + ", World!"));
    
  • thenCombine:组合两个 CompletableFuture 的结果。

    CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello");
    CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "World");
    CompletableFuture<String> combinedFuture = future1.thenCombine(future2, (result1, result2) -> result1 + ", " + result2 + "!");
    

异常处理

CompletableFuture 提供了多种方法来处理异步任务中的异常:

  • exceptionally:在任务完成异常时提供一个替代值。

    CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
        if (true) {
            throw new RuntimeException("Exception occurred!");
        }
        return "Hello";
    }).exceptionally(ex -> "Recovered from exception");
    
  • handle:在任务完成后处理结果或异常。

    CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
        if (true) {
            throw new RuntimeException("Exception occurred!");
        }
        return "Hello";
    }).handle((result, ex) -> {
        if (ex != null) {
            return "Recovered from exception";
        }
        return result;
    });
    

应用场景

  • 异步执行 I/O 密集型任务:例如,读取文件、网络请求等,这些操作通常耗时较长,使用 CompletableFuture 可以避免阻塞主线程。
  • 并行处理多个任务:例如,多个独立的任务可以并行执行,并在它们都完成后处理结果。
  • 复杂的异步任务组合:例如,一个任务的结果需要作为下一个任务的输入,或者多个任务的结果需要组合在一起。

示例

以下是一个使用 CompletableFuture 的示例,展示了如何进行异步任务的组合和异常处理。

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class CompletableFutureExample {
    public static void main(String[] args) {
        // 异步任务1:模拟网络请求
        CompletableFuture<String> networkRequest = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(1000); // 模拟延迟
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "Network Response";
        });

        // 异步任务2:模拟文件读取
        CompletableFuture<String> fileRead = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(500); // 模拟延迟
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "File Content";
        });

        // 组合任务:等待所有任务完成,然后组合结果
        CompletableFuture<String> combinedFuture = networkRequest.thenCombine(fileRead, (networkResult, fileResult) -> {
            return "Combined Result: " + networkResult + " + " + fileResult;
        });

        // 异常处理
        combinedFuture = combinedFuture.exceptionally(ex -> "Error: " + ex.getMessage());

        // 等待结果并打印
        try {
            String result = combinedFuture.get();
            System.out.println(result);
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }
}

解释

  1. 定义异步任务:使用 supplyAsync 方法创建两个异步任务,分别模拟网络请求和文件读取。
  2. 组合任务:使用 thenCombine 方法组合两个任务的结果。
  3. 异常处理:使用 exceptionally 方法处理可能的异常。
  4. 获取结果:使用 get 方法等待任务完成并获取结果。

通过这些示例和解释,你可以了解到 CompletableFuture 的强大功能以及在异步编程中的应用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值