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();
            }
        }
    
    }
  • 相关阅读:
    【JAVA基础】private 的使用
    【nginx】配置文件(模块、反向代理、负载均衡、动静分离)
    【Nginx】命令行安装
    【UNIAPP】websocte实现,功能:指定房间聊天,匿名进入 功能,文字与图片
    【前端JS】input对象图片在线转base64
    【UNIAPP】上传视频,进度条的前台与后端
    【IO阻塞异步】协程使用异步,异步爬虫,异步数据库操作
    【装饰器】原理以及基础使用
    可编程网络DataPath 及XDP
    gitlab 代码协作流程
  • 原文地址:https://www.cnblogs.com/moris5013/p/12032234.html
Copyright © 2011-2022 走看看