CompletableFuture使用

首先,查看该类的接口结构

1.初始化(是否有返回值,是否要指定线程池产生4种方法)

CompletableFuture.runAsync(Runnable runnable);
CompletableFuture.runAsync(Runnable runnable, Executor executor);
 
CompletableFuture.supplyAsync(Supplier<U> supplier);
CompletableFuture.supplyAsync(Supplier<U> supplier, Executor executor)

默认情况下 CompletableFuture 会使用公共的 ForkJoinPool 线程池,这个线程池默认创建的线程数是 CPU 的核数(也可以通过 JVM option:-Djava.util.concurrent.ForkJoinPool.common.parallelism 来设置 ForkJoinPool 线程池的线程数)。如果所有 CompletableFuture 共享一个线程池,那么一旦有任务执行一些很慢的 I/O 操作,就会导致线程池中所有线程都阻塞在 I/O 操作上,从而造成线程饥饿,进而影响整个系统的性能。所以,强烈建议要根据不同的业务类型创建不同的线程池,以避免互相干扰。

 

2. get方法和join方法的区别:get方法返回结果,抛出的是检查异常,必须用户throw或者try/catch处理,join返回结果,抛出未检查异常

T get() throws InterruptedException, ExecutionException{}

T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException

//Returns the result value when complete, or throws an  (unchecked) exception if completed //exceptionally.
public T join()

3.描述串行操作API

//既能接收参数也支持返回值
CompletionStage<R> thenApply(fn);
CompletionStage<R> thenApplyAsync(fn);

//接收参数不返回值
CompletionStage<Void> thenAccept(consumer);
CompletionStage<Void> thenAcceptAsync(consumer);

//不接收参数 不返回值
CompletionStage<Void> thenRun(action);
CompletionStage<Void> thenRunAsync(action);
//会新创建出一个子流程,最终结果和 thenApply是相同

CompletionStage<R> thenCompose(fn);
CompletionStage<R> thenComposeAsync(fn);
   CompletableFuture<String> f1 =
                CompletableFuture.supplyAsync(() -> "a")
                        .thenApply(String::toUpperCase)
                        .thenCompose(i -> CompletableFuture.supplyAsync(() -> i + " B"));
        //输出A B
        System.out.println(f1.join());

4.汇聚接口API

//整合两个计算结果,只是入参和返回结果不同方法不同
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);

//任意一个结束就返回
CompletionStage applyToEither(other, fn);
CompletionStage applyToEitherAsync(other, fn);

CompletionStage acceptEither(other, consumer);
CompletionStage acceptEitherAsync(other, consumer);

CompletionStage runAfterEither(other, action);
CompletionStage runAfterEitherAsync(other, action);

//所有任务完成才返回
static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs)
//有一个任务完成就返回其值 
static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs) 

5.异常

//类似于try{}catch{}中的 catch{}
CompletionStage exceptionally(fn);

//类似于 try{}finally{}中的 finally{}
//无返回结果的
CompletionStage<R> whenComplete(consumer);
CompletionStage<R> whenCompleteAsync(consumer);
//有返回结果 
CompletionStage<R> handle(fn);
CompletionStage<R> handleAsync(fn);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值