zoukankan      html  css  js  c++  java
  • CompletableFuture2

    public class CompletableFuture2 {
    
        public static void main(String[] args) throws InterruptedException {
    //      thenAcceptBoth();
    //      acceptEither();
    //      runAfterBoth();
    //      runAfterEither();
    //      combine();
            compose();
            Thread.currentThread().join();
        }
    
    
        /**
         * 前一个CompletableFuture执行完后的结果,作为参数传递给后面的Function
         */
        private static  void  compose(){
            CompletableFuture.supplyAsync(() -> {
                System.out.println("start  the compose1");
                sleep(5);
                System.out.println("end  the compose1");
                return "hello1";
            }).thenCompose((s) -> CompletableFuture.supplyAsync(() -> {
                System.out.println("start  the compose2");
                sleep(3);
                System.out.println("end  the compose2");
                return s.length();
            })).thenAccept(System.out::println);
    
        }
    
        /**
         * 两个都执行完后,再执行后面的BiFunction,BiFunction是有两个参数一个返回值的
         */
        private static  void  combine(){
            CompletableFuture.supplyAsync(() -> {
                System.out.println("start  the combine1");
                sleep(5);
                System.out.println("end  the combine1");
                return "hello1";
            }).thenCombine(CompletableFuture.supplyAsync(() -> {
                System.out.println("start  the combine2");
                sleep(3);
                System.out.println("end  the combine2");
                return 100;
            }), (s, i) ->   s.length() > i
            ).whenComplete((v, t) -> {
                System.out.println(v);
            });
    
    
        }
    
    
        /**
         *  其中有任何一个执行完后,执行后面的runnable
         */
        private static  void  runAfterEither(){
            CompletableFuture.supplyAsync(() -> {
                System.out.println("start  the runAfterEither1");
                sleep(5);
                System.out.println("end  the runAfterEither1");
                return "hello1";
            }).runAfterEither(CompletableFuture.supplyAsync(() -> {
                System.out.println("start  the runAfterEither2");
                sleep(3);
                System.out.println("end  the runAfterEither2");
                return "hello1";
            }),()-> System.out.println("======"));
        }
    
        /**
         * 两个都执行完后,执行后面的runnable
         */
        private static  void  runAfterBoth(){
            CompletableFuture.supplyAsync(() -> {
                System.out.println("start  the runAfterBoth1");
                sleep(5);
                System.out.println("end  the runAfterBoth1");
                return "hello1";
            }).runAfterBoth(CompletableFuture.supplyAsync(() -> {
                System.out.println("start  the runAfterBoth2");
                sleep(3);
                System.out.println("end  the runAfterBoth2");
                return "hello1";
            }), () ->  System.out.println("=====")
            );
        }
    
    
        /**
         * 其中有任何一个执行完后,执行后面的consumer
         */
        private static  void  acceptEither(){
            CompletableFuture.supplyAsync(() -> {
                System.out.println("start  the acceptEither1");
                sleep(5);
                System.out.println("end  the acceptEither1");
                return "hello1";
            }).acceptEither(CompletableFuture.supplyAsync(() -> {
                System.out.println("start  the acceptEither2");
                sleep(3);
                System.out.println("end  the acceptEither2");
                return "hello2";
            }), (s) -> {
                System.out.println(s);
            });
    
        }
    
    
        /**
         * thenAcceptBoth 两个都执行完后,执行后面的BiConsumer,BiConsumer两个参数,无返回值
         */
        private static  void  thenAcceptBoth(){
            CompletableFuture.supplyAsync(()->{
                System.out.println("start  the supplyAsync");
                sleep(5);
                System.out.println("end  the supplyAsync");
                return "hello";
            }).thenAcceptBoth(CompletableFuture.supplyAsync(()->{
                System.out.println("start  the thenAcceptBoth");
                sleep(3);
                System.out.println("end  the thenAcceptBoth");
                return 100;
            }),(s,i)->{
                System.out.println(s+" ===" + i);
            });
        }
    
        private static  void  sleep(int sec){
            try {
                TimeUnit.SECONDS.sleep(sec);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
    }
  • 相关阅读:
    NET CORE EF事务
    搭建Vue-nuxt.js
    VUE获取URL(导航)参数方法
    第十二届蓝桥杯大赛软件赛决赛题解
    第十二届蓝桥杯大赛软件赛省赛第二场题解
    P1955 [NOI2015] 程序自动分析
    P1621 集合
    将博客搬至CSDN
    2021第六届GPLT 团体程序设计天梯赛CCCC 个人题解
    Divide by Zero 2021 and Codeforces Round #714 (Div. 2)
  • 原文地址:https://www.cnblogs.com/moris5013/p/12032234.html
Copyright © 2011-2022 走看看