码迷,mamicode.com
首页 > 其他好文 > 详细

CompletableFuture2

时间:2019-12-12 23:40:50      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:bic   err   enc   class   exception   cep   返回   执行   tac   

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();
        }
    }

}

 

CompletableFuture2

标签:bic   err   enc   class   exception   cep   返回   执行   tac   

原文地址:https://www.cnblogs.com/moris5013/p/12032234.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!