CompletableFuture

CompletableFuture 是 Java 中用于处理异步任务的一个强大工具,它提供了丰富的方法来管理异步任务的完成、组合以及异常处理。下面是一些常见的 CompletableFuture 方法的用法和示例:

创建 CompletableFuture

CompletableFuture.supplyAsync(Supplier<U> supplier)

这个方法接受一个 Supplier 参数,返回一个 CompletableFuture,它会在一个新的线程中执行这个 Supplier,并返回结果。

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
    // 异步任务逻辑
    return "Hello";
});
CompletableFuture.runAsync(Runnable runnable)

这个方法接受一个 Runnable 参数,返回一个 CompletableFuture<Void>,它会在一个新的线程中执行这个 Runnable。

CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
    // 异步任务逻辑
    System.out.println("Async task running");
});

链式调用

thenApply(Function<? super T,? extends U> fn)

在异步任务完成后,对任务的结果应用一个函数,并返回一个新的 CompletableFuture

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello")
        .thenApply(result -> result + " World");
thenAccept(Consumer<? super T> action)

在异步任务完成后,对任务的结果执行一个操作,但不返回新的结果。

CompletableFuture.supplyAsync(() -> "Hello")
        .thenAccept(result -> System.out.println("Received: " + result));
thenRun(Runnable action)

在异步任务完成后,执行一个操作,不接受任务结果。

CompletableFuture.supplyAsync(() -> "Hello")
        .thenRun(() -> System.out.println("Async task completed"));

组合多个 CompletableFuture

thenCombine(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn)

组合两个 CompletableFuture 的结果,对结果应用一个函数,并返回一个新的 CompletableFuture

CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello");
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "World");

CompletableFuture<String> combinedFuture = future1.thenCombine(future2, (result1, result2) -> result1 + " " + result2);
thenCompose(Function<? super T,? extends CompletionStage<U>> fn)

对一个异步任务的结果进行处理,返回一个新的 CompletableFuture,并且这个新的 CompletableFuture 依赖于原始任务的结果。

CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello");
CompletableFuture<String> future2 = future1.thenCompose(result -> CompletableFuture.supplyAsync(() -> result + " World"));

异常处理

exceptionally(Function<Throwable, ? extends T> fn)

处理异步任务的异常情况,并返回一个默认值或者补偿结果。

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
    // 异步任务逻辑,可能会抛出异常
    throw new RuntimeException("Error occurred");
}).exceptionally(ex -> "Handled Exception: " + ex.getMessage());
handle(BiFunction<? super T, Throwable, ? extends U> fn)

在异步任务完成或者抛出异常时进行处理,并返回一个新的结果。

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
    // 异步任务逻辑,可能会抛出异常
    throw new RuntimeException("Error occurred");
}).handle((result, ex) -> {
    if (ex != null) {
        return "Handled Exception: " + ex.getMessage();
    } else {
        return result;
    }
});

这些只是 CompletableFuture 类提供的一些方法中的一部分,还有许多其他方法可供使用,这些方法可以满足更复杂的异步编程需求。

  • 11
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

墨辰李

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值