Java CompletableFuture 异步并发

1. CompletableFuture 异步编程

1.1 使用工厂方法supplyAsync创建CompletableFuture

        CompletableFuture<String> aFuture = CompletableFuture.supplyAsync(()->doSomeThingA());
        CompletableFuture<String> bFuture = CompletableFuture.supplyAsync(()->doSomeThingB());
        
        try {
            String aResult = aFuture.get(2, TimeUnit.SECONDS);
            String bResult = bFuture.get();

            System.out.println(aResult+ ", " + bResult);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
  • get 方法

定义Future的异步返回,通过supplyAsync一行代码实现异步操作,会将任务提交给ForkJoinPool的线程池进行操作。其中get可以设置超时,避免一直等待,如果超时了会抛出java.util.concurrent.TimeoutException异常。

  • join 方法

join方法和get方法使用类似,阻塞获取值,区别在于 join() 抛出的是 unchecked Exception。

  • runAsync 方法

runAsync 方法接收的是 Runnable 的实例,意味着它没有返回值

  • supplyAsync 方法

upplyAsync 方法对应的是有返回值的情况, 方法的带 executor 的变种,表示让任务在指定的线程池中执行,不指定的话,通常任务是在 ForkJoinPool.commonPool() 线程池中执行的。

1.2 实施串行关系

先执行A,再执行B。

CompletionStage<R> thenApply(fn);
CompletionStage<R> thenApplyAsync(fn);
CompletionStage<Void> thenAccept(consumer);
CompletionStage<Void> thenAcceptAsync(consumer);
CompletionStage<Void> thenRun(action);
CompletionStage<Void> thenRunAsync(action);
CompletionStage<R> thenCompose(fn);
CompletionStage<R> thenComposeAsync(fn);

参考例子如下:

CompletableFuture<String> strFuture = CompletableFuture.supplyAsync(() -> "ABC").thenApply(s -> s+ "D").thenApply(String::toLowerCase);

System.out.println(strFuture.join());

1.2 多任务组合执行

  • AND 组合关系
CompletionStage<R> thenCombine(other, fn);
CompletionStage<R> thenCombineAsync(other, fn);
CompletionStage<Void> thenAcceptBoth(other, consumer);
CompletionStage<Void> thenAcceptBothAsync(other, consumer);
CompletionStage<Void> runAfterBoth(other, action);
CompletionStage<Void> runAfterBothAsync(other, action);

具体实施例子如下:

        CompletableFuture<String> aFuture = CompletableFuture.supplyAsync(()->doSomeThingA());
        CompletableFuture<String> bFuture = CompletableFuture.supplyAsync(()->doSomeThingB());

        try {

            aFuture.thenAcceptBoth(bFuture, (aResult, bResult)->{
                System.out.println(aResult+ ", " + bResult);
            });
            /*
            CompletableFuture<String> cResult = aFuture.thenCombine(bFuture, (aResult, bResult)->{
                return aResult + "," + bResult;
            });
            System.out.println(cResult.get());
             */

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
  • OR组合关系
CompletionStage applyToEither(other, fn);
CompletionStage applyToEitherAsync(other, fn);
CompletionStage acceptEither(other, consumer);
CompletionStage acceptEitherAsync(other, consumer);
CompletionStage runAfterEither(other, action);
CompletionStage runAfterEitherAsync(other, action);

具体实施例子如下:

cfA.acceptEither(cfB, result -> {});
cfA.acceptEitherAsync(cfB, result -> {});
cfA.acceptEitherAsync(cfB, result -> {}, executorService);
 
cfA.applyToEither(cfB, result -> {return result;});
cfA.applyToEitherAsync(cfB, result -> {return result;});
cfA.applyToEitherAsync(cfB, result -> {return result;}, executorService);
 
cfA.runAfterEither(cfA, () -> {});
cfA.runAfterEitherAsync(cfB, () -> {});
cfA.runAfterEitherAsync(cfB, () -> {}, executorService);
  • allOf 和 anyOf
public static CompletableFuture<Void> 	    allOf(CompletableFuture<?>... cfs)
public static CompletableFuture<Object> 	anyOf(CompletableFuture<?>... cfs)

实现例子1 : 异步调用,等全部结果返回,统一处理。

    public static int i = 0;

    static CompletableFuture<String> doTask(String taskId) {
        return CompletableFuture.supplyAsync(() -> {
            // mock task
            try {
                System.out.println(Thread.currentThread().getName() + ":start!");
                Thread.sleep((++i) * 1000);
                System.out.println(Thread.currentThread().getName() + ":end!");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return "task" + taskId;
        });
    }

    public static void batchProcess(List<String> taskIdList) {
        // 并行task
        List<CompletableFuture<String>> taskFutureList = taskIdList.stream().map(taskId -> doTask(taskId)).collect(Collectors.toList());

        // 使用allOf标识所有的并行任务
        CompletableFuture<Void> allFuture = CompletableFuture.allOf(taskFutureList.toArray(new CompletableFuture[taskFutureList.size()]));

        // 获取所有子任务的处理结果
        CompletableFuture<List<String>> finalResults = allFuture.thenApply(v -> taskFutureList.stream()
                .map(taskFuture -> taskFuture.join()).collect(Collectors.toList()));

        try {
            List<String> l = finalResults.get();
            System.out.println(l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }

实现例子2:

    public static int i = 0;

    static <T> T doTask(T taskId) {
        // mock task
        try {
            System.out.println(Thread.currentThread().getName() + ":start!");
            Thread.sleep((++i) * 1000);
            System.out.println(Thread.currentThread().getName() + ":end!");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return taskId;
    }

    public static <T> List<T> batchProcess(List<T> taskList, ExecutorService executor) {
        List<CompletableFuture<T>> futures = new ArrayList<>();
        for (int i = 0; i < taskList.size(); i++) {
            final int j = i;
            //异步执行
            CompletableFuture<T> future = CompletableFuture.supplyAsync(() -> doTask(taskList.get(j)), executor)
                    //如需获取任务完成先后顺序,此处代码即可
                    .whenComplete((v, e) -> {
                        System.out.println("任务" + v + "完成!result=" + v + ",异常 e=" + e + "," + new Date());
                    });
            futures.add(future);
        }

        //1.构造一个空CompletableFuture,子任务数为入参任务list size
        CompletableFuture<Void> allDoneFuture = CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()]));

        try {
            //2.流式(总任务完成后,每个子任务join取结果,后转换为list)
            return allDoneFuture.thenApply(v -> futures.stream().map(CompletableFuture::join).collect(Collectors.toList())).get();
        }catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }


// main函数调用

 ExecutorService executor = Executors.newFixedThreadPool(10);
        List<String> taskList = new ArrayList<>();
        taskList.add("A");
        taskList.add("B");
        List<String> result = batchProcess(taskList, executor);
        result.forEach(k -> {
            System.out.println(k);
        });

1.3 异常处理

  • whenComplete(…)

功能:当CompletableFuture的计算结果完成,或者抛出异常的时候,都可以进入whenComplete方法执行

public CompletionStage<T> whenComplete(BiConsumer<? super T, ? super Throwable> action);
public CompletionStage<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action);
public CompletionStage<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action,Executor executor);

实现的例子如下:

        CompletableFuture<String> future = CompletableFuture.supplyAsync(()-> "执行结果:" + (100 / 0))
                .thenApply(s -> "apply result:" + s)
                .whenComplete((s, e) -> {
                   if (s != null) {
                       System.out.println(s); // 未执行
                   }
                   if (e == null) {
                       System.out.println(s); // 未执行
                   } else {
                       System.out.println(e.getMessage());//java.lang.ArithmeticException: / by zero
                   }
                }).exceptionally(e -> {
                    System.out.println("ex: "+e.getMessage()); //ex:java.lang.ArithmeticException: / by zero
                    return "futureA result: 100";
                });

        System.out.println(future.join());
  • handle(…)

功能:当CompletableFuture的计算结果完成,或者抛出异常的时候,可以通过handle方法对结果进行处理

public <U> CompletionStage<U> handle(BiFunction<? super T, Throwable, ? extends U> fn);
public <U> CompletionStage<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn);
public <U> CompletionStage<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn,Executor executor);

实施的例子如下:

CompletableFuture<String> futureA = CompletableFuture.
                supplyAsync(() -> "执行结果:" + (100 / 0))
                .thenApply(s -> "apply result:" + s)
                .handle((s, e) -> {
                    if (e == null) {
                        System.out.println(s);//未执行
                    } else {
                        System.out.println(e.getMessage());//java.lang.ArithmeticException: / by zero
                    }
                    return "handle result:" + (s == null ? "500" : s);
                })
                .exceptionally(e -> {
                    System.out.println("ex:" + e.getMessage()); //未执行
                    return "futureA result: 100";
                });
System.out.println(futureA.join());//handle result:500
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值