查看: 8|回复: 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.

在本版发帖返回顶部