• CompletableFuture 和 Future 的选择,以及CompletableFuture的用法


    Java 编程中,异步编程是一种重要的技术,它允许你在执行长时间运行的任务时不会阻塞主线程。为了支持异步编程,Java 提供了 FutureCompletableFuture 这两个关键的类。在本文中,我们将比较它们的特点、优缺点以及使用场景。

    Future 的特点:
    1. 特点

      • Future 是 Java 5 引入的接口,用于表示一个异步计算的结果。
      • Future 可以通过 get 方法来获取异步操作的结果,但该方法是阻塞的,如果异步操作未完成,它会一直等待。
      • Future 不提供一种直接的方式来添加回调函数,处理操作完成后的结果或异常。
      • Future 只能表示异步任务是否完成,而不能手动触发任务的完成或组合多个任务。
    2. 优点

      • 简单易用,适用于基本的异步操作。
    3. 缺点

      • 阻塞问题:get 方法会阻塞当前线程,如果异步任务很慢或永远不完成,会导致程序出现长时间阻塞。
      • 缺乏异步编程的灵活性:难以编写复杂的异步代码,例如组合多个异步任务或处理异常。
    4. 使用场景

      • 适用于简单的异步任务,不需要处理复杂的回调或组合多个任务的结果。
    CompletableFuture 的特点:
    1. 特点

      • CompletableFuture 是 Java 8 引入的类,是 Future 的增强版。
      • 支持非阻塞的异步操作,可以使用回调函数(例如 thenApplythenCompose)来处理操作完成后的结果。
      • 可以手动触发任务的完成,使得编写自定义的异步逻辑更加灵活。
      • 支持组合多个异步任务的结果。
    2. 优点

      • 异步编程更加灵活和强大,可以处理复杂的异步操作。
      • 可以避免阻塞,提高程序性能。
    3. 缺点

      • 学习曲线较陡峭,相对复杂,需要更多的理解和实践。
      • 需要小心管理线程池和资源,以避免资源泄漏或性能问题。
    4. 使用场景

      • 适用于复杂的异步编程场景,需要组合多个异步任务,处理异常,或者避免阻塞。
      • 需要更多控制和灵活性的情况,如超时处理、异步事件处理等。

    CompletableFuture 的使用方法:

    现在让我们深入了解一下 CompletableFuture 的使用方法。

    创建 CompletableFuture:

    你可以使用不同的方式创建 CompletableFuture 对象,最常见的是使用 CompletableFuture.supplyAsyncCompletableFuture.runAsync 方法,这些方法接受一个 SupplierRunnable 作为参数,并返回一个 CompletableFuture 对象。

    CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 42);
    CompletableFuture<Void> futureVoid = CompletableFuture.runAsync(() -> doSomething());
    
    • 1
    • 2
    异步操作:

    CompletableFuture 允许你执行异步操作,这些操作不会阻塞当前线程。你可以将需要执行的操作包装在 CompletableFuture 中,然后使用 thenApplythenComposethenAccept 等方法来定义操作完成后的回调。

    CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 42);
    CompletableFuture<String> futureResult = future.thenApply(result -> "Result: " + result);
    
    • 1
    • 2
    组合 CompletableFuture:

    你可以将多个 CompletableFuture 组合在一起,以便在它们都完成时执行某个操作,或者在其中任何一个完成时执行某个操作。这可以使用 thenCombinethenComposethenCombineAsync 等方法来实现。

    CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> 42);
    CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> 23);
    CompletableFuture<Integer> combinedFuture = future1.thenCombine(future2, (result1, result2) -> result1 + result2);
    
    • 1
    • 2
    • 3
    异常处理:

    CompletableFuture 允许你处理异步操作中可能发生的异常。你可以使用 exceptionallyhandle 方法来捕获和处理异常。

    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
    });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    等待 CompletableFuture 完成:

    你可以使用 get 方法等待 CompletableFuture 的完成,但要小心,因为它会阻塞当前线程,直到任务完成。

    CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 42);
    try {
       Integer result = future.get();
       System.out.println("Result: " + result);
    } catch (Exception e) {
       // Handle exceptions
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    组合多个 CompletableFuture:

    使用 CompletableFuture.allOfCompletableFuture.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
    
    • 1
    • 2
    • 3
    • 4
    • 5
  • 相关阅读:
    2023上海应用技术大学计算机考研信息汇总
    Tomcat 集群介绍
    C# String转DateTime
    【GNN】2022 G-Mixup: Graph Data Augmentation for Graph Classification
    【Spring系列】- Spring循环依赖
    信息获取技术复习
    [剑指Offer] 三种方法求解找出数组中出现次数超过一半的数字
    C/C++函数调用过程的汇编代码分析(arm64指令集)
    DNS工作原理分析
    云原生微服务治理经典框架之Spring Cloud Alibaba核心技术与实战案例
  • 原文地址:https://blog.csdn.net/m0_51663233/article/details/133625136