CompletableFuture 实战模板(超时、组合、异常链处理)

这篇给你可直接复用的模板,不讲大道理,讲"真实项目里怎么写才稳"。

先说结论

在项目里写 CompletableFuture,最容易出问题的就三件事:

  1. 没有超时(线程一直卡住)
  2. 异常没兜底(主线程卡死 or 悄悄失败)
  3. 线程池乱用(公共池被打爆)

解决思路:有超时、有兜底、有隔离的线程池


一、基础模板:统一线程池 + 超时 + 兜底

java 复制代码
ExecutorService bizPool = new ThreadPoolExecutor(
    16, 32, 60, TimeUnit.SECONDS,
    new LinkedBlockingQueue<>(2000),
    new ThreadFactoryBuilder().setNameFormat("biz-%d").build(),
    new ThreadPoolExecutor.CallerRunsPolicy()
);

CompletableFuture<User> future = CompletableFuture
    .supplyAsync(() -> userService.getById(uid), bizPool)
    .orTimeout(800, TimeUnit.MILLISECONDS)
    .exceptionally(ex -> {
        log.warn("userService fallback, uid={}", uid, ex);
        return User.DEFAULT;
    });

这个模板适合 80% 的"单个异步调用"。


二、并行查询聚合(allOf)

需求:并发查用户、订单、积分

java 复制代码
CompletableFuture<User> f1 = CompletableFuture.supplyAsync(() -> getUser(uid), bizPool);
CompletableFuture<Order> f2 = CompletableFuture.supplyAsync(() -> getOrder(uid), bizPool);
CompletableFuture<Point> f3 = CompletableFuture.supplyAsync(() -> getPoint(uid), bizPool);

CompletableFuture<Void> all = CompletableFuture.allOf(f1, f2, f3);

UserProfile profile = all.thenApply(v -> {
    return new UserProfile(f1.join(), f2.join(), f3.join());
}).orTimeout(1, TimeUnit.SECONDS)
  .exceptionally(ex -> UserProfile.fallback(uid))
  .join();

注意:join() 会把异常包装成 CompletionException,一定要有兜底。


三、竞速返回(anyOf)

需求:多个数据源取最快结果

java 复制代码
CompletableFuture<Result> f1 = CompletableFuture.supplyAsync(() -> queryA(key), bizPool);
CompletableFuture<Result> f2 = CompletableFuture.supplyAsync(() -> queryB(key), bizPool);

Result result = CompletableFuture.anyOf(f1, f2)
    .orTimeout(300, TimeUnit.MILLISECONDS)
    .thenApply(r -> (Result) r)
    .exceptionally(ex -> Result.empty())
    .join();

四、串行依赖(thenCompose)

需求:先查用户,再查用户的订单

java 复制代码
CompletableFuture<Order> future = CompletableFuture
    .supplyAsync(() -> getUser(uid), bizPool)
    .thenCompose(user -> CompletableFuture.supplyAsync(
        () -> getOrder(user.getId()), bizPool
    ))
    .orTimeout(800, TimeUnit.MILLISECONDS)
    .exceptionally(ex -> Order.EMPTY);

五、异常链处理(handle vs exceptionally)

java 复制代码
CompletableFuture<String> f = CompletableFuture
    .supplyAsync(() -> doWork(), bizPool)
    .handle((res, ex) -> {
        if (ex != null) {
            log.warn("work failed", ex);
            return "fallback";
        }
        return res;
    });

handle 能同时拿到结果和异常,适合统一兜底。


六、避免公共线程池被打爆

不要直接用 CompletableFuture.supplyAsync() 默认线程池,

它用的是 ForkJoinPool.commonPool,很容易被其他任务占满。

结论:业务异步一定要用自定义线程池。


七、一个可直接复用的"组合模板"

java 复制代码
CompletableFuture<UserProfile> profileFuture = CompletableFuture
    .supplyAsync(() -> getUser(uid), bizPool)
    .thenCombineAsync(
        CompletableFuture.supplyAsync(() -> getOrder(uid), bizPool),
        (user, order) -> new UserProfile(user, order),
        bizPool
    )
    .orTimeout(800, TimeUnit.MILLISECONDS)
    .exceptionally(ex -> UserProfile.fallback(uid));

最后总结

CompletableFuture 最靠谱的姿势是:

  1. 统一线程池
  2. 每段链路加超时
  3. 异常统一兜底
  4. 聚合前后都可回退

只要把这四件事做对,你的异步代码会稳很多。

相关推荐
十年Java程序媛1 分钟前
深度实战:JDK21 虚拟线程 + HikariCP 生产正确配比、坑点、监控全套
java·spring boot
xiaoqiMikko9 分钟前
有人在搜一个不存在的 Tomcat 版本
java·tomcat
SelectDB3 小时前
为什么 JSON 正在成为分析数据库新的竞争点?
数据库·json·agent
ClouGence3 小时前
开源数据库管理工具 CloudDM 4.2.0 发布,新增 GoldenDB、KingbaseES 等数据源
数据库·dba·devops
用户3126874877204 小时前
ConcurrentHashMap 怎么保证线程安全?从分段锁到 CAS+synchronized
java
发霉的馒头4 小时前
ORA-00845: MEMORY_TARGET not supported on this system的解决方法
数据库
vipxieliang4 小时前
ValidX vs Apache Commons Validator:功能与性能对比
java·spring boot
SimonKing4 小时前
升级Spring Boot 4后,从 Jackson 2 到 3,到底有哪些变化
java·后端·程序员
吃饱了得干活4 小时前
一篇讲清楚Spring Boot:自动装配、启动器、过滤器、拦截器、设计模式
java·spring boot·后端
lhldsg6 小时前
AI零售系统实战指南:从架构设计到落地部署全解析
java·人工智能·小程序·uni-app·零售