CompletableFuture的使用

1、CompletableFuture的静态方法,创建CompletableFuture实例

(1)runAsync(Runnable runnable),其中future.get方法会阻塞,无返回值

CompletableFuture future = CompletableFuture.runAsync(() ->{
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("runasync");
        });
        System.out.println(11111);
        future.get();
        System.out.println(2222222);


(2)supplyAsync(Supplier<U> supplier),该方法有返回值

CompletableFuture future = CompletableFuture.supplyAsync(() -> {
            System.out.println("supplyAsync:" + Thread.currentThread());
            return "supply";
        });
System.out.println(future.get());

(3)上述两个静态方法使用的线程池是默认线程池,如果需要指定线程池,可以使用如下两个方法

runAsync(Runnable runnable, Executor executor)
supplyAsync(Supplier<U> supplier, Executor executor)

2、对于Future,提交任务后,只能调用get()等待返回结果,CompletableFuture可以在结果上加上一个callback,当得到结果后,再执行callback

(1)thenRun(Runnable runable)

CompletableFuture future = CompletableFuture
                .runAsync(() -> System.out.println("runasync"),executor)
                .thenRun(() -> System.out.println("thenRun"));
        future.get();
        System.out.println("end");

(2)thenAccept (Consumer<? super T> action),可以消费上一个任务的返回结果,但thenAccept本身没有返回值

 CompletableFuture future = CompletableFuture.supplyAsync(() -> {
            System.out.println("supplyAsync:" + Thread.currentThread());
            return "supply";
        }).thenAccept(str -> System.out.println("thenapplay:" + str.length()));
        System.out.println(future.get()); // 无返回值

(3)thenApply(Function fn),与thenAccept相似可以处理上一个任务的返回值,但thenApply可以有自己的返回值

CompletableFuture future = CompletableFuture.supplyAsync(() -> {
            System.out.println("supplyAsync:" + Thread.currentThread());
            return "supply";
        }).thenApply(str -> {
                    System.out.println("thenapplay:" + Thread.currentThread());
                    return str.length();
                });
        System.out.println(future.get()); // 返回6

3、thenCompose与thenCombine

(1) thenCompose(Function<? super T, ? extends CompletionStage<U>> fn)将两个异步操作流进行流水线

 CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("supplyAsync:" + Thread.currentThread());
            return "supply";
        }).thenCompose(str -> CompletableFuture.supplyAsync(str::length)); 
// 此处是第二个异步线程,thenApply执行的任务不是异步的,是在主线程中完成
        System.out.println(future.get());// 返回 6

(2)thenCombine(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn),将两个异步线程的结果进行处理

 CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("supplyAsync:" + Thread.currentThread());
            return "supply";
        }).thenCombine(CompletableFuture.supplyAsync(() -> 6), (str, length) -> str.length() + length);
        System.out.println(future.get()); // 返回12 ,将第一个任务返回的"supply"的长度加上第二个任务返回的6

4、任意多个CompletableFuture的组合

(1)public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs),表示"与"操作,返回的是CompletableFuture<Void>,会等到所有任务都完成才结束

(2)public static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs),表示"或"操作,返回的是CompletableFuture<Object>,只要有一个任务完成,就结束

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值