CompleteFuture全方法说明

1. 创建CompletableFuture

函数名备注
supplyAsync参数Supplier 无参数有返回值
runAsync参数Runnable 无参数无返回值
@Slf4j
public class MyTest {
    private final Executor executors = Executors.newFixedThreadPool(10);
    
    @Test
    public void test01() throws ExecutionException, InterruptedException {
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 1, executors);
        CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> System.out.println("Runnable"),executors);
        System.out.println(future.get());
    }
}

2. 串行化方法

函数名支持异步(带Async后缀)备注
thenApply有参数有返回值 Function
thenAccept有参数无返回值 Consumer
thenRun无参数无返回值 Runnable

不带有Async表示默认使用当前线程(主线程执行)
带有Async后缀的表示可以使用其他的线程池执行异步任务,默认使用ForkJoinPool,可以进行指明自己的线程池(推荐)

@Slf4j
public class MyTest {
    private final Executor executors = Executors.newFixedThreadPool(10);
    private Consumer<Object> consumer = i -> log.info(String.valueOf(i));
    
    @Test
    public void test02() throws ExecutionException, InterruptedException {
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 1, executors);
        future.thenApply(i -> i + 1);
        future.thenAccept(consumer);
        future.thenRun(() -> log.info("Hello"));
        future.thenAcceptAsync(consumer, executors); // 推荐使用
    }
}

3. 任务最终处理

函数名支持异步(带Async后缀)备注
whenComplete仅处理结果返回原始CompletableFuture
exceptionally×仅处理异常并返回默认值
handle×处理结果和异常并返回默认值

如果中间步骤出现异常,后续的链式链接不起作用,直接走到异常部分,并返回默认值

@Slf4j
public class MyTest {
    private final Executor executors = Executors.newFixedThreadPool(10);
    private Consumer<Object> consumer = i -> log.info(String.valueOf(i));
    
    @Test
    public void test03() {
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 1, executors);
        future.whenComplete((i, throwable) -> {
            consumer.accept(i);
            consumer.accept(throwable);
        });
        future.thenApply(integer -> {consumer.accept(integer); return 10/ 0;})
                .exceptionally(throwable -> {
                    consumer.accept(throwable);
                    return 10;
                });
        future.thenApply(integer -> 10 / 0)
                .handle((integer, throwable) -> {
                    consumer.accept(integer);
                    consumer.accept(throwable);
                    return 10;
                });
    }
}

4. 任务组合

两个任务组合

函数名支持异步(带Async后缀)备注
thenCombine处理两个结果并返回值
thenAcceptBoth处理两个结果无返回值
runAfterBoth无参数无返回结果
@Slf4j
public class MyTest {
    private final Executor executors = Executors.newFixedThreadPool(10);
    private Consumer<Object> consumer = i -> log.info(String.valueOf(i));
    
    @Test
    public void test04() {
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 1, executors);
        CompletableFuture<Integer> newFuture = CompletableFuture.supplyAsync(() -> 2, executors);
        future.thenCombineAsync(newFuture, (integer, integer2) -> integer + integer2, executors);
        future.thenAcceptBothAsync(newFuture, (integer, integer2) -> { consumer.accept(integer + integer2);});
        future.runAfterBothAsync(newFuture, () -> log.info("Runnable"));
    }
}

任意一个任务完成

函数名支持异步(带Async后缀)备注
applyToEither处理两个结果并返回值
acceptEither处理两个结果无返回值
runAfterEither无参数无返回结果
@Slf4j
public class MyTest {
    private final Executor executors = Executors.newFixedThreadPool(10);
    private Consumer<Object> consumer = i -> log.info(String.valueOf(i));
    
    @Test
    public void test04() {
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 1, executors);
        CompletableFuture<Integer> newFuture = CompletableFuture.supplyAsync(() -> 2, executors);
        future.applyToEither(newFuture, integer -> integer);
        future.acceptEither(newFuture, integer -> consumer.accept(integer));
        future.runAfterEither(newFuture, () -> log.info("Runnable"));
    }
}

扁平化任务

函数名支持异步(带Async后缀)备注
thenCompose×类似于Stream的flatMap
@Slf4j
public class MyTest {
    private final Executor executors = Executors.newFixedThreadPool(10);
    private Consumer<Object> consumer = i -> log.info(String.valueOf(i));
    
    @Test
    public void test04() {
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 1, executors);
        CompletableFuture<Integer> newFuture = CompletableFuture.supplyAsync(() -> 2, executors);
        future.thenCompose(integer -> CompletableFuture.supplyAsync(() -> integer * 10).thenApply(i -> i / 10));
    }
}

多任务组合

函数名支持异步(带Async后缀)备注
allOf×静态方法,完成全部任务
anyOf×静态方法,完成任意一个任务
@Slf4j
public class MyTest {
    private final Executor executors = Executors.newFixedThreadPool(10);
    private Consumer<Object> consumer = i -> log.info(String.valueOf(i));
    
    @Test
    public void test04() {
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 1, executors);
        CompletableFuture<Integer> newFuture = CompletableFuture.supplyAsync(() -> 2, executors);
        CompletableFuture<Void> allOf = CompletableFuture.allOf(future, newFuture);
        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(future, newFuture);
    }
}
  • 1
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值