查看: 25|回覆: 0

CompletableFuture 实战:Java 异步编程高性能实战指南

[複製鏈接]

2

主題

1

回帖

0

積分

热心网友

金币
1
閲讀權限
220
精華
0
威望
0
贡献
0
在線時間
0 小時
註冊時間
2011-1-3
發表於 2026-3-30 12:15:00 | 顯示全部樓層 |閲讀模式

前言

在高并发场景下,同步阻塞是性能杀手。Java 8 引入的 CompletableFuture 彻底改变了异步编程的写法。

一、为什么需要 CompletableFuture?

传统 Future 的痛点:

  • future.get() 会阻塞当前线程
  • 无法链式组合多个异步任务
  • 异常处理繁琐

二、基础用法

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
  try { Thread.sleep(1000); } catch (InterruptedException e) { }
  return "查询结果";
});

三、实战场景

电商首页需要同时查询用户信息、商品推荐、优惠券列表。串行查询耗时 650ms,并行只需 300ms。

@Service
public class HomePageService {
  private final ExecutorService executor = Executors.newFixedThreadPool(20);
  
  public HomePageVO buildHomePage(Long userId) {
    CompletableFuture<UserInfo> userFuture = CompletableFuture
      .supplyAsync(() -> userService.getUserInfo(userId), executor);
    CompletableFuture<List<Product>> productFuture = CompletableFuture
      .supplyAsync(() -> productService.getRecommendations(userId), executor);
    CompletableFuture<List<Coupon>> couponFuture = CompletableFuture
      .supplyAsync(() -> couponService.getAvailableCoupons(userId), executor);
    
    CompletableFuture.allOf(userFuture, productFuture, couponFuture).join();
    
    return HomePageVO.builder()
      .userInfo(userFuture.join())
      .products(productFuture.join())
      .coupons(couponFuture.join())
      .build();
  }
}

四、异常处理

CompletableFuture<String> future = CompletableFuture
  .supplyAsync(() -> { if (Math.random() > 0.5) throw new RuntimeException("异常"); return "成功"; })
  .exceptionally(ex -> { log.error("任务异常", ex); return "默认值"; })
  .handle((result, ex) -> { if (ex != null) return "处理异常"; return "处理结果: " + result; });

五、最佳实践

  1. 必须自定义线程池
  2. 避免 get() 阻塞
  3. 异常不能吞掉

总结

CompletableFuture 是 Java 异步编程的利器。核心记住:supplyAsync 创建任务、thenApply/thenCompose 编排任务、exceptionally 处理异常。

回覆

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即注册

本版積分規則

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部