CompletableFuture

thenCombine() // 两个任务一起执行完成后结果汇总

  public static void thenCombine() {
        CompletableFuture<String> fi = CompletableFuture.supplyAsync(() -> {
            ThreadUtil.printThreadInfo("厨师炒菜");
            ThreadUtil.sleep(200);
            return "大头菜";
        }).thenCombine(CompletableFuture.supplyAsync(() -> {
            ThreadUtil.printThreadInfo("服务员煮饭");
            ThreadUtil.sleep(100);
            return " 米饭";
        }), (a, b) -> {
            ThreadUtil.printThreadInfo("服务员拿菜和饭");
            ThreadUtil.sleep(50);
            return a + b;
        });
        ThreadUtil.printThreadInfo("吃饭 " + fi.join());
    }

allOf() 多个任务执行结果合并

public static void allOf() {
        List<String> stringList = Lists.newArrayList();
        CompletableFuture<String>[] list = new CompletableFuture[3];
        CompletableFuture<String> f1 = CompletableFuture.supplyAsync(() -> {
            ThreadUtil.sleep(300);
            ThreadUtil.printThreadInfo("厨师炒菜");
            return "大头菜";
        }).whenComplete((v, e) -> {
            stringList.add(v);
        });
        list[0] = f1;
        CompletableFuture<String> f2 = CompletableFuture.supplyAsync(() -> {
            ThreadUtil.sleep(200);
            ThreadUtil.printThreadInfo("服务员煮饭");
            return " 煮饭";
        }).whenComplete((v, e) -> {
            stringList.add(v);
        });
        list[1] = f2;
        CompletableFuture<String> f3 = CompletableFuture.supplyAsync(() -> {
            ThreadUtil.sleep(100);
            ThreadUtil.printThreadInfo("服务员打饭");
            return " 打饭";
        }).whenComplete((v, e) -> {
            stringList.add(v);
        });
        list[2] = f3;
        CompletableFuture.allOf(list).join();
        stringList.forEach(str->{
            ThreadUtil.printThreadInfo(str);
        });
    }

anyOf() 多个任务中一个执行完就返回

public static void anyOf() {
        List<String> stringList = Lists.newArrayList();
        CompletableFuture<String>[] list = new CompletableFuture[3];
        CompletableFuture<String> f1 = CompletableFuture.supplyAsync(() -> {
            ThreadUtil.sleep(300);
            ThreadUtil.printThreadInfo("厨师炒菜");
            return "大头菜";
        }).whenComplete((v, e) -> {
            stringList.add(v);
        });
        list[0] = f1;
        CompletableFuture<String> f2 = CompletableFuture.supplyAsync(() -> {
            ThreadUtil.sleep(200);
            ThreadUtil.printThreadInfo("服务员煮饭");
            return " 煮饭";
        }).whenComplete((v, e) -> {
            stringList.add(v);
        });
        list[1] = f2;
        CompletableFuture<String> f3 = CompletableFuture.supplyAsync(() -> {
            ThreadUtil.sleep(100);
            ThreadUtil.printThreadInfo("服务员打饭");
            return " 打饭";
        }).whenComplete((v, e) -> {
            stringList.add(v);
        });
        list[2] = f3;
        CompletableFuture.anyOf(list).join();
        stringList.forEach(str->{
            ThreadUtil.printThreadInfo(str);
        });
    }

applyToEither() 两个任务谁先执行完谁就返回

public static void applyToEither() {
        CompletableFuture<String> fi = CompletableFuture.supplyAsync(() -> {
            ThreadUtil.printThreadInfo("厨师炒菜开始");
            ThreadUtil.sleep(200);
            ThreadUtil.printThreadInfo("厨师炒菜完成");
            return "大头菜";
        }).applyToEither(CompletableFuture.supplyAsync(() -> {
            ThreadUtil.printThreadInfo("服务员打饭开始");
            ThreadUtil.sleep(100);
            ThreadUtil.printThreadInfo("服务员打饭完成");
            return " 米饭";
        }),a->a);
        ThreadUtil.printThreadInfo("吃饭 " + fi.join());
    }

exceptionally() 异常处理

    public static void exceptionally() {
        CompletableFuture<String> fi = CompletableFuture.supplyAsync(() -> {
            ThreadUtil.printThreadInfo("厨师炒菜开始");
            ThreadUtil.sleep(200);
            ThreadUtil.printThreadInfo("厨师炒菜完成");
            return "大头菜";
        }).applyToEither(CompletableFuture.supplyAsync(() -> {
            ThreadUtil.printThreadInfo("服务员打饭开始");
            ThreadUtil.sleep(100);
            ThreadUtil.printThreadInfo("服务员打饭完成");
            return "米饭";
        }),a->{
            ThreadUtil.printThreadInfo("异常");
            if(a.equals("米饭")){
                throw new RuntimeException("程序异常");
            }
            return "正常";
        }).exceptionally(e->{
            ThreadUtil.printThreadInfo("异常程序终止");
            return "程序终止";
        });
        ThreadUtil.printThreadInfo("吃饭 " + fi.join());
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值