多线程之CompletableFuture(中)

thenAcceptAsync

Consumer 接收上个阶段的返回值T,return Void;返回CompletableFuture<Void>

  • thenAccept
    该方法是同步的
public static void main(String[] args) {
        CompletableFuture<Void> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync---------开始---");
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync---------结束---");
            return "hello";
        }).thenAccept(s -> {
            System.out.println(Thread.currentThread().getName() + "---thenAcceptAsync---start---" + s);
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "---thenAcceptAsync----end---" + s);
        });
        try {
            System.out.println("============================");
            stringCompletableFuture.join();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

代码结果:这里写图片描述
thenAccept方法是mian线程执行的,阻塞了输出“============================”。

  • thenAcceptAsync
    以Async结尾代表是异步,将上述代码中thenAccept换成thenAcceptAsync,看输出结果:
    这里写图片描述

下面介绍的方法都会有以Async和没有Async结尾两种形式,都是类似的,以Async结尾代表异步,统一只介绍以Async结尾的方法。


thenRunAsync

Runnable 不接受上个阶段返回值和异常,return Void;返回CompletableFuture<Void>

public static void main(String[] args) {
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync---------开始---");
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync---------结束---");
            return "hello";
        }).thenRunAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "---thenRunAsync---start---");
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "---thenRunAsync----end---");
        });
        try {
            System.out.println(voidCompletableFuture.join());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

这里写图片描述


thenapplyAsync

Function<T, R>接收上个阶段的返回值T,return R ; 返回CompletableFuture<R>

public static void main(String[] args) {
        CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
            return "hello";
        }).thenApplyAsync(v -> {
            System.out.println(Thread.currentThread().getName() + "---thenApplyAsync---start---" + v);
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "---thenApplyAsync----end---" + v);
            return v.length();
        });
        try {
            System.out.println(completableFuture.get());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

这里写图片描述


whencompleteAsync

BiConsumer<T, U> 接收上个阶段的返回值T和异常U,return Void;返回上个阶段返回值CompletableFuture<T>

public static void main(String[] args) {
        CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync-----------");
            return "hello";
        }).whenCompleteAsync((V, T) -> {
            System.out.println(Thread.currentThread().getName() + "---whenCompleteAsync---start---" + V);
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "---whenCompleteAsync----end---" + V);
        });
        try {
            System.out.println(stringCompletableFuture.get());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

这里写图片描述


handleAsync

BiFunction<T, U, R> 接收上个阶段的返回值T和异常U,return R;返回CompletableFuture<R>

public static void main(String[] args) {
        CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync---------开始---");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "---------supplyAsync---------结束---");
            int e = 2/0;
            return "hello";
        }).handleAsync((s, v) -> {
            System.out.println(Thread.currentThread().getName() + "---handle---start---" + s);
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "---handle----end---" + s);
            System.out.println("捕获的异常:"+ v);
            return s == null ? 0 : s.length();
        });
        try {
            System.out.println(completableFuture.get());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值