高并发编程-异步-JDK8-CompletableFuture(二)

高并发编程-异步-JDK8-CompletableFuture(二)

六、结果消费

  1、thenAccept

  与thenApply和thenCompose不同的是,结果消费没有返回一个CompletableFuture,只执行了执行结果Action,没有任何新值返回。

  根据对结果的处理方式,消费函数分为:

  • thenAccept是对单个的结果进行消费
  • thenAcceptBoth只对两个结果进行消费
  • thenRun不关心结果,只执行Action

  thenAccept有一下三种方法,函数接口为Consumer,他只能输入而没有输出

public CompletionStage<Void> thenAccept(Consumer<? super T> action);
public CompletionStage<Void> thenAcceptAsync(Consumer<? super T> action);
public CompletionStage<Void> thenAcceptAsync(Consumer<? super T> action,Executor executor);

  使用实例

public static void main(String[] args) throws InterruptedException {
        thenAcceptDemo();
        countDownLatch.await();
    }
    public  static void thenAcceptDemo(){
        CompletableFuture<Void> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
            int num = new Random().nextInt(30);
            System.out.println(num);
            return num;
        }).thenAccept(number ->{

            System.out.println("第二阶段:" + number * 5);
            countDownLatch.countDown();
        });
    }

        2、thenAcceptBoth

  thenAcceptBoth 函数的作用是,当两个 CompletionStage 都正常完成计算的时候,就会执 行提供的action消费两个异步的结果

public <U> CompletionStage<Void> thenAcceptBoth(CompletionStage<? extends U> other,BiConsumer<? super T, ? super U> action);
public <U> CompletionStage<Void> thenAcceptBothAsync(CompletionStage<? extends U> other,BiConsumer<? super T, ? super U> action);
public <U> CompletionStage<Void> thenAcceptBothAsync(CompletionStage<? extends U> other,BiConsumer<? super T, ? super U> action,Executor executor);

  使用实例

public static void main(String[] args) throws InterruptedException {
        thenAcceptBothDemo();
        countDownLatch.await();

    }
public  static void thenAcceptBothDemo(){
        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
            @Override
            public Integer get() {
                int number = new Random().nextInt(3) + 1;
                System.out.println("第一次执行得到的值" + number);
                return number;
            }
        });
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
            @Override
            public Integer get() {
                int number = new Random().nextInt(3) + 5;
                System.out.println("第二次执行得到的值" + number);
                return number;
            }
        });
future1.thenAcceptBoth(future2, new BiConsumer<Integer, Integer>() {
            @Override
            public void accept(Integer integer, Integer integer2) {
                countDownLatch.countDown();
                System.out.println("计算的结果为"+(integer+integer2));
            }
        });
    }

   3、thenRun

  thenRun也是对线程结果的一种消费,但是和thenAccept不同的是他会在上一个CompletableFuture执行完后,会执行一个Runnable,但是Runnable并没有获取执行结果。

 public CompletionStage<Void> thenRun(Runnable action);

 public CompletionStage<Void> thenRunAsync(Runnable action);

 public CompletionStage<Void> thenRunAsync(Runnable action,Executor executor);

  使用实例

  

 public static void main(String[] args) throws InterruptedException {
        thenRunDemo();
        countDownLatch.await();

    }
    @SneakyThrows
    public static  void  thenRunDemo(){
        CompletableFuture<Void> futrue = CompletableFuture.supplyAsync(() -> {
            int number = new Random().nextInt(3) + 1;
            return number;
        }).thenRun(() -> {
            countDownLatch.countDown();
            System.out.println("不接收上面的数值");

        });
        System.out.println(futrue.get());
    }

七、结果组合

  thenCombine

  合并两个线程任务的结果,进行下一步处理

public <U,V> CompletableFuture<V> thenCombine(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn) {
        return biApplyStage(null, other, fn);
    }
public <U,V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn) {
        return biApplyStage(asyncPool, other, fn);
    }

public <U,V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn,
Executor executor) {
return biApplyStage(screenExecutor(executor), other, fn); }

  使用实例

    public static void main(String[] args) throws InterruptedException {
        thenCombine();
        countDownLatch.await();


    }
    @SneakyThrows
    public static void thenCombine(){
        Executor e = Executors.newFixedThreadPool(2);
        CompletableFuture<Integer> futrue1 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
            @Override
            public Integer get() {
                int sum = new Random().nextInt(10);
                System.out.println("线程" + Thread.currentThread().getName() + "执行操作:" + sum);
                return sum;
            }
        },e);
        CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(new Supplier<Integer>() {

            @Override
            public Integer get() {
                int sum = new Random().nextInt(15);
                System.out.println("线程" + Thread.currentThread().getName() + "执行操作:" + sum);
                return sum;
            }
        },e);

        CompletableFuture<Integer> totalnumber = futrue1.thenCombine(future2, new BiFunction<Integer, Integer, Integer>() {
            @Override
            public Integer apply(Integer sum1, Integer sum2) {
                System.out.println("线程" + Thread.currentThread().getName() + "执行操作:"+sum1+"=="+sum2);
                return sum1 + sum2;
            }
        });

        System.out.println("最后的计算结果为:"+totalnumber.get());
    }

 

  

  

 

posted @ 2021-12-22 11:15  小亲年  阅读(139)  评论(0)    收藏  举报