从零开始学习CompletableFuture(一)基本使用

使用场景

​ 在日常开发中,我们常常在开启多线程来提升程序执行的速度,但是在多个线程运行后,我们可能需要获取运行的结果进行汇总

首先想到的是使用CountDownLatch工具类,不同线程运行后使用countDown+记录的方式来获取运行结果。这种方式能完成我们的需求但是在速度和返回值获取上并没有达到最优效果

​ jdk1.8之后提供了一个功能强大的类,支持异步回调,且线程并行,那就是CompletableFuture。

基本使用

CompletableFuture实现了CompletionStage接口和Future接口,前者是对后者的一个扩展,增加了异步回调、流式处理、多个Future组合处理的能力,使Java在处理多任务的协同工作时更加顺畅便利。

创建异步任务 supplyAsync / runAsync
  • supplyAsync表示创建带返回值的异步任务
//有返回值
        CompletableFuture<String> future=CompletableFuture.supplyAsync(() -> {
            String uuid = UUID.randomUUID().toString();
            System.out.println(Thread.currentThread().getName()+"正在执行一个有返回值的异步任务。");
            System.out.println(uuid);
            return uuid;
        });
        CompletableFuture<String> result1 = future.thenApply((result)->{
            System.out.println(Thread.currentThread().getName()+"thenApply1");
            return getUuid();
        });

        System.out.println(Thread.currentThread().getName()+"  结果:"+result1.get());
  • runAsync表示创建无返回值的异步任务
CompletableFuture<Void> future=CompletableFuture.runAsync(() -> {
            try {
                System.out.println(Thread.currentThread().getName()+"正在执行一个没有返回值的异步任务。");
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        try {
            future.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+" 结束。");
执行异步回调
thenApply / thenApplyAsync

thenApply 表示某个任务执行完成后执行的动作

@Test
    public void test5() throws Exception {
        ForkJoinPool pool=new ForkJoinPool();
        // 创建异步执行任务:
        CompletableFuture<Double> cf = CompletableFuture.supplyAsync(()->{
            System.out.println(Thread.currentThread()+" start job1,time->"+System.currentTimeMillis());
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
            System.out.println(Thread.currentThread()+" exit job1,time->"+System.currentTimeMillis());
            return 3.25;
        },pool);
        //cf关联的异步任务的返回值作为方法入参,传入到thenApply的方法中
        //thenApply内部uniApplyStage方法创建了一个新的CompletableFuture实例
        CompletableFuture<String> cf2=cf.thenApply((result)->{
            System.out.println(Thread.currentThread()+" start job2,time->"+System.currentTimeMillis());
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
            System.out.println(Thread.currentThread()+" exit job2,time->"+System.currentTimeMillis());
            return "thenApply:"+result;
        });
        System.out.println("main thread start cf.get(),time->"+System.currentTimeMillis());
        //等待子任务执行完成
        System.out.println("run result->"+cf.get());
        System.out.println("main thread start cf2.get(),time->"+System.currentTimeMillis());
        System.out.println("run result->"+cf2.get());
        System.out.println("main thread exit,time->"+System.currentTimeMillis());
    }

thenApplyAsync与thenApply的区别

  • thenApplyAsync将job2提交到线程池中异步执行,实际执行job2的线程可能是另外一个线程;

  • thenApply是由执行job1的线程立即执行job2,即两个job都是同一个线程执行的。

thenAccept / thenRun
  • thenAccept 接收上一个任务的执行结果作为入参,但是没有返回值
  • thenRun 无入参,也没有返回值
exceptionally

某个任务执行异常时执行的回调方法,会将抛出异常作为参数传递到回调方法中,如果该任务正常执行则会exceptionally方法返回的CompletionStage的result就是该任务正常执行的结果

@Test
    public void test2() throws Exception {
        ForkJoinPool pool=new ForkJoinPool();
        // 创建异步执行任务:
        CompletableFuture<Double> cf = CompletableFuture.supplyAsync(()->{
            System.out.println(Thread.currentThread()+"job1 start,time->"+System.currentTimeMillis());
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
            if(true){
                throw new RuntimeException("test");
            }else{
                System.out.println(Thread.currentThread()+"job1 exit,time->"+System.currentTimeMillis());
                return 1.2;
            }
        },pool);
        //cf执行异常时,将抛出的异常作为入参传递给回调方法
        CompletableFuture<Double> cf2= cf.exceptionally((param)->{
            System.out.println(Thread.currentThread()+" start,time->"+System.currentTimeMillis());
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
            System.out.println("error stack trace->");
            param.printStackTrace();
            System.out.println(Thread.currentThread()+" exit,time->"+System.currentTimeMillis());
            return -1.1;
        });
        //cf正常执行时执行的逻辑,如果执行异常则不调用此逻辑
        CompletableFuture cf3=cf.thenAccept((param)->{
            System.out.println(Thread.currentThread()+"job2 start,time->"+System.currentTimeMillis());
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
            System.out.println("param->"+param);
            System.out.println(Thread.currentThread()+"job2 exit,time->"+System.currentTimeMillis());
        });
        System.out.println("main thread start,time->"+System.currentTimeMillis());
        //等待子任务执行完成,此处无论是job2和job3都可以实现job2退出,主线程才退出,如果是cf,则主线程不会等待job2执行完成自动退出了
        //cf2.get时,没有异常,但是依然有返回值,就是cf的返回值
        System.out.println("run result->"+cf2.get());
        System.out.println("main thread exit,time->"+System.currentTimeMillis());
    }
whenComplete

whenComplete是当某个任务执行完成后执行的回调方法,会将执行结果或者执行期间抛出的异常传递给回调方法,如果是正常执行则异常为null,回调方法对应的CompletableFuture的result和该任务一致,如果该任务正常执行,则get方法返回执行结果,如果是执行异常,则get方法抛出异常。

组合处理
thenCombine / thenAcceptBoth / runAfterBoth

将两个CompletableFuture组合起来,只有这两个都正常执行完了才会执行某个任务

  • thenCombine会将两个任务的执行结果作为方法入参传递到指定方法中,且该方法有返回值
  • thenAcceptBoth同样将两个任务的执行结果作为方法入参,但是无返回值
  • runAfterBoth没有入参,也没有返回值。
applyToEither / acceptEither / runAfterEither

将两个CompletableFuture组合起来,只要其中一个执行完了就会执行某个任务

  • applyToEither会将已经执行完成的任务的执行结果作为方法入参,并有返回值
  • acceptEither同样将已经执行完成的任务的执行结果作为方法入参,但是没有返回值
  • runAfterEither没有方法入参,也没有返回值。
thenCompose

henCompose方法会在某个任务执行完成后,将该任务的执行结果作为方法入参然后执行指定的方法,该方法会返回一个新的CompletableFuture实例,如果该CompletableFuture实例的result不为null,则返回一个基于该result的新的CompletableFuture实例,然后执行这个新任务

 CompletableFuture<Void> cf2= cf.thenCompose((param)-> CompletableFuture.runAsync(()->{
            System.out.println(param);
        }));

对比thenApply

  • thenapply()是返回的是非CompletableFuture类型
  • thenCompose()用来连接两个CompletableFuture,返回值是新的CompletableFuture
allOf / anyOf
  • allOf返回的CompletableFuture是多个任务都执行完成后才会执行
  • anyOf返回的CompletableFuture是多个任务只要其中一个执行完成就会执行

使用示例

public class CompletableFutureAllof {

    private final static Logger log = LoggerFactory.getLogger(CompletableFutureAllof.class);

    public static void main(String[] args) {
        List<CompletableFuture<String>> completableFutureList = Lists.newCopyOnWriteArrayList();
        log.info("start");
        for (int i=0;i<5;i++){
            CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(()->getUUid());

            completableFutureList.add(completableFuture);
        }

        List<String> resultList= Lists.newCopyOnWriteArrayList();
        List<CompletableFuture<String>> cresultList= Lists.newCopyOnWriteArrayList();
        for (CompletableFuture<String> future:completableFutureList){
            CompletableFuture<String> completableFuture = future.whenComplete((result, throwable) -> {


                if (Objects.nonNull(result)) {
                    System.out.println(Thread.currentThread());
                    resultList.add(result);
                }
                if (Objects.nonNull(throwable)) {
                    log.error("error");
                }

                
            });
            cresultList.add(completableFuture);
        }
        //list转array
        CompletableFuture<List<String>>[] completableFutures = new CompletableFuture[cresultList.size()];
        CompletableFuture.allOf(cresultList.toArray(completableFutures)).join();

        System.out.println(resultList.size());

    }


    /**
     * 模拟接口返回,sleep后返回uuid
     * @return
     */
    public static String getUUid(){
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return UUID.randomUUID().toString();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值