CompletableFuture学习

//supplyAsync 和 runAsync 区别在前个有返回值,后一个没有返回值
    public static void supplyAsync() throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newCachedThreadPool();
//        executorService.submit(() -> {
//            System.out.println("executorService 是否为守护线程 :" + Thread.currentThread().isDaemon());
//            return null;
//        });

        CompletableFuture<String> completableFuture1=CompletableFuture.supplyAsync(()->{
            System.out.println("this is lambda supplyAsync");
            System.out.println("supplyAsync 是否为守护线程 " + Thread.currentThread().isDaemon());
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "result1";
        });

        CompletableFuture<String> completableFuture2=CompletableFuture.supplyAsync(()->{
            System.out.println("this is  executorService");
            System.out.println("executorService  supplyAsync 是否为守护线程 " + Thread.currentThread().isDaemon());
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "result2";
        },executorService);
        System.out.println(completableFuture1.get());
        System.out.println(completableFuture2.get());
        executorService.shutdown();
    }

//这两个方法的入参是一个completableFuture组、allOf就是所有任务都完成时返回。但是是个Void的返回值。
//anyOf是当入参的completableFuture组中有一个任务执行完毕就返回。返回结果是第一个完成的任务的结果
    public static void of() throws ExecutionException, InterruptedException {
        CompletableFuture<String> completableFuture1=CompletableFuture.supplyAsync(()->{
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "result1";
        });
        CompletableFuture<String> completableFuture2=CompletableFuture.supplyAsync(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "result2";
        });
        CompletableFuture allOf = CompletableFuture.allOf(completableFuture1,completableFuture2);
        System.out.println(allOf.get());
        CompletableFuture anyOf = CompletableFuture.anyOf(completableFuture1,completableFuture2);
        System.out.println(anyOf.get());
    }

    //这个方法是执行这个方法的时候任务执行完了就返回任务的结果,如果任务没有执行完就返回你的入参。
    public static void getNow() throws ExecutionException, InterruptedException {
        CompletableFuture<String> completableFuture1=CompletableFuture.supplyAsync(()->{
            try {
                TimeUnit.SECONDS.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "result1";
        });
        System.out.println(completableFuture1.getNow("222"));//输出:222
    }

    /*
    whenComplete:如果调用whenComplete的中途,还发生了其他事情,图中的主线程的sleep(400);导致completableFuture这个任务执行完毕了,
    那么就使用主线程调用。如果调用的中途没有发生其他任务且在触碰到whenComplete方法时completableFuture这个任务还没有彻底执行完毕那么就会用completableFuture这个任务所使用的线程。
    whenCompleteAsync: 异步执行
     */
    public static void whenXXX() throws ExecutionException, InterruptedException {
        CompletableFuture<String> completableFuture1=CompletableFuture.supplyAsync(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread());
            return "result1";
        }).whenCompleteAsync((result,ex)->{
            System.out.println(result+"-"+ex+"-"+Thread.currentThread());
        });
        System.out.println("-"+Thread.currentThread());
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(completableFuture1.get()+"-"+Thread.currentThread());
    }
    /*
    使用任务的线程接着执行后续的操作
    thenCompose: 上一个任务结果是then的一部分
    thenCombine: 2个任务的结果合并到一个方法里
    thenApply: 有入参有返回值类型的
    thenAccept : 有入参没有返回值类型
     */
    public static void thenXXX() throws ExecutionException, InterruptedException {
        CompletableFuture<String> completableFuture1=CompletableFuture.supplyAsync(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread());
            return "result1";
        });
        CompletableFuture<String> thenCompose=completableFuture1.thenCompose(s -> CompletableFuture.supplyAsync(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(s+"-"+Thread.currentThread());
            return s+":"+"thenCompose";
        }));//s=="result1"
        CompletableFuture<String> thenCombine=completableFuture1.thenCombine( CompletableFuture.supplyAsync(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("thenCombine"+"-"+Thread.currentThread());
            return "thenCombine";
        }),(s1,s2)-> s1+":"+s2 );//2个任务的结果合并到一个方法里

        System.out.println(completableFuture1.get());
        System.out.println(thenCompose.get());
        System.out.println(thenCombine.get());

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值