线程的异步编排

多个异步任务之间的协调,分布式系统下,多个信息来自很多远程服务,这时候需要协调这些异步返回接口

1、指定使用哪个线程池执行

(1)无返回值的

        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
            System.out.println("当前线程:" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果:" + i);
        }, executor);

(2)有返回值的,因为是提供一个supply供给型接口

        CompletableFuture<Integer> supplyCompletableFuture = CompletableFuture.supplyAsync(() -> {
                    System.out.println("当前线程:" + Thread.currentThread().getId());
                    int i = 10 / 2;
                    System.out.println("运行结果:" + i);
                    return i;
                }
                , executor);
        
    }
    # 拿取返回值,主线程会阻塞
    Integer integer = supplyCompletableFuture.get();
    System.out.println(integer);

2、完成时回调(方法带Async,由其它线程执行,但使用同一个池还是有可能被同线程调用,不带Async的方法则由上一个线程继续执行)

(1)当这个执行完,接下来继续干什么

        CompletableFuture<Integer> supplyCompletableFuture = CompletableFuture.supplyAsync(() -> {
                    System.out.println("当前线程:" + Thread.currentThread().getId());
                    int i = 10 / 2;
                    System.out.println("运行结果:" + i);
                    try {
                        Thread.sleep(1000*6);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    return i;
                }
                , executor).whenComplete((res,exception)->{
            System.out.println("结果"+res);
            System.out.println("异常"+exception);

        });

whenComplete方法内部第一个参数是消费型接口传入的是上步线程的结果,第二个发生了异常的对象

(2)优化异常问题,因为whenComplete虽然能拿到异常但修改不了你没完成的数据,所以使用exceptionally,参数是一个函数式接口,丢入异常对象,返回最终返回值,相当于服务降级机制

        CompletableFuture<Integer> supplyCompletableFuture = CompletableFuture.supplyAsync(() -> {
                    System.out.println("当前线程:" + Thread.currentThread().getId());
                    int i = 10 / 2;
                    System.out.println("运行结果:" + i);
                    try {
                        Thread.sleep(1000*6);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    return i;
                }
                , executor).whenComplete((res,exception)->{
            System.out.println("结果"+res);
            System.out.println("异常"+exception);

        }).exceptionally(exception->{
                return 10;
        });

3、handle 方法,对whenComplete的增强,因为whenComplete是结果、异常的感知,handle 则可修改处理返回数据

        CompletableFuture<Integer> supplyCompletableFuture = CompletableFuture.supplyAsync(() -> {
                    System.out.println("当前线程:" + Thread.currentThread().getId());
                    int i = 10 / 2;
                    System.out.println("运行结果:" + i);
                    try {
                        Thread.sleep(1000*6);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    return i;
                }
                , executor).handle((res,excption)->{
                    if(res!=null){
                        return res++;
                    }
                    return 0;
        });

4、一个任务干完了,再处理,线程的串行

老样子,带ASync开新线程,不带ASync用之前同一个线程,使用形式和上面的大致相同

(1)thenApply方法:获取上个任务的值,自己再重新返回值

(2)thenAccept方法:获取上个任务的值只消费,自己不返回值

(2)thenRun方法:单纯的等上步任务执行完后,只要上面的任务执行完成,就开始执行 thenRun,不获取值,不返回值

5、两任务组合,意思就是先让2个任务执行完,再处理,上面的描述是对于一个任务而言的,这便是对先等2个而言的。

接口参数大致都是第一个参数是组合第二个任务的Runable接口,第二个参数即要等2任务执行完后再处理的Runable接口
(1)thenCombine:获取上2任务的值,自己处理再重新返回值
(2)thenAcceptBoth:获取上2任务的值只消费,自己不返回值
(3)runAfterBoth:单纯的等这2任务执行完后,就开始执行,自己不返回值

6、两任务组合,只要其中有一个行了,我便再处理

(1)applyToEither:获取上成功那个的值,自己处理再重新返回值
(2)acceptEither:获取上成功那个的值只消费,自己不返回值
(3)runAfterEither:单纯的等这有1个任务成功后,就开始执行,自己不返回值

6、多任务组合

因为方法参数是多参数的CompletableFuture类型,可以组合多个任务,
(1)allOf:等待所有任务完成,无返回值
(2)anyOf:只要有一个任务完成,有返回值

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值