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();
            }
        }
    
    }
  • 相关阅读:
    51nod乘积之和
    Dell服务器安装OpenManage(OMSA)
    Nginx反向代理PHP
    搭建haproxy
    108. Convert Sorted Array to Binary Search Tree
    60. Permutation Sequence
    142. Linked List Cycle II
    129. Sum Root to Leaf Numbers
    118. Pascal's Triangle
    26. Remove Duplicates from Sorted Array
  • 原文地址:https://www.cnblogs.com/moris5013/p/12032234.html
Copyright © 2011-2022 走看看