前言
在高并发场景下,同步阻塞是性能杀手。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; });
复制代码 五、最佳实践
- 必须自定义线程池
- 避免 get() 阻塞
- 异常不能吞掉
总结
CompletableFuture 是 Java 异步编程的利器。核心记住:supplyAsync 创建任务、thenApply/thenCompose 编排任务、exceptionally 处理异常。
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |