CompletableFuture异步线程

 1、线程池七大参数介绍

(1)corePoolSize:线程池中常驻核心线程数

(2)maximumPoolSize:线程池能够容纳同时执行的最大线程数,此值必须大于等于1

(3)keepAliveTime:多余的空闲线程存活时间。当前线程池数量超过corePoolSize时,当空闲时间到达keepAliveTime值时,多余空闲线程会被销毁直到只剩下corePoolSize个线程为止。

(4)unit:keepAliveTime的时间单位

(5)BlockingQueue:阻塞队列,如果任务很多,就将在目前多的任务放在队列里。只要有空闲,就回去队列里去除最新的任务执行。

(6)threadFactory:表示生成线程池中的工作线程的线程工厂,用于创建线程,一般为默认线程工厂即可

(7)handler:拒绝策略,表示当队列满了并且工作线程大于等于线程池的最大线程数(maximumPoolSize)时如何来拒绝来请求的Runnable的策略

 

2、运行顺序

1、线程池创建,准备好core数量的核心线程,准备接受任务。

2、新的任务进来,用core准备好的空闲线程执行。

(1)、core满了,就将再进来的任务放入阻塞队列中。空闲的core就会自己去阻塞队

列获取任务执行

(2)、阻塞队列满了,就直接开新线程执行,最大只能开到ma指定的数量

(3)、max都执行好了。max-core数量空闲的线程会在 keepAliveTime指定的时间后自

动销毁。最终保持到core大小

(4)、如果线程数开到了max的数量,还有新任务进来,就会使用 reject指定的拒绝策

略进行处理

3、所有的线程创建都是由指定的 factory创建的。

 

3、异步对象

 CompletableFuture提供了四个静态方法来创建一个异步操作。

 1、static CompletableFuture<void> runAsync(Runnable runnable)

 2、public static CompletableFuture <void> runAsync(Runnable runnable, Executor executor)

3、public static <U> CompletableFuture<U> SupplyAsync(Supplier <U> supplier)

4、 public static <U> CompletableFuture<U> supplyAsync(Supplier <U> supplier, Executor executor)

 runXxoox都是没有返回结果的, supplyXox都是可以获取返回结果的

 

4、计算完成时回调方法 

public CompletableFuture<T> whenCompleteBiConsumer<? super T,? super Throwable> action);

public CompletableFuture<T> whenCompleteAsyn(BiConsumer<? super T,? super Throwable>action):

public CompletableFuture<T> whenCompleteAsyncBiconsumer<? super T,? super Throwable>action, Executor executor);

public CompletableFuture<T> exceptionallyFunction<Throwable, extends T> fn);

 whenComplete可以处理正常和异常的计算结果但是无法修改返回数据, exceptionally处理异常情况并且可以修改返回数据。

 whenComplete和 whenCompleteAsync的区别:

  •  whenComplete:是执行当前任务的线程执行继续执行 whenComplete的任务。
  •  whenCompleteAsync:是执行把 whenCompleteAsync这个任务继续提交给线程池来进行执行。

方法不以 Async结尾,意味着 Action使用相同的线程执行,而 Async可能会使用其他线程执行(如果是使用相同的线程池,也可能会被同一个线程选中执行)

 

5、 handle方法

 public Completionstage< handleBiFunction<? super T, Throwable, extends U> fn):

 public <U> CompletionstagecU> handleAsyneBiFunction<? super T, Throwable, extends U> fn);

 public <U> CompletionstagecU> handleAsyncBiFunction<? super T, Throwable, extends U> fn, Executor executor);

  • 和 complete一样,可对结果做最后的处理(可处理异常),可改变返回值。

 

6、两任务组合-都要完成

 public <U,V> CompletableFuturec <V> thenCombine( Completionstage< ? extends U> other, BiFunction<? super T,? super U,? extends> fn);

 public<U,V> CompletableFuture <V> thenCombineAsync( CompletionStage<? extends> other, BiFunction<? super T.? super U,? extends> fn);

 public <U,V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends> fn, Executor executor);

 public <U> CompletableFuture<Void> thenAcceptBoth(Completionstage<? extends U> other,Biconsumer< super T, super> action);

 public <U> CompletableFuturevoid <Void> thenAcceptBothasync(Completionstage<? extends U> other, Biconsumer<? super T, super U> action);

 public <U> CompletableFuture<Void> thenAcceptBothasync(Completionstage<? extends U> other,Biconsumer<? super T, super Uaction, Executor executor);

 public CompletableFuture<Void> runAfterBotl(Completionstage<?> other,Runnable action);

 public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action);

 public CompletableFuture<Void> runAfterBothAsync(Completionstage<?> other,Runnable action, Executor executor);

  • 两个任务必须都完成,触发该任务。
  •  thenCombine:组合两个 future,获取两个 future的返回结果,并返回当前任务的返回值;
  • thenAcceptBoth:组合两个 future,获取两个 future任务的返回结果,然后处理任务,没有返回值;
  •  runAfterBoth:组合两个 future,不需要获取 future的结果,只需两个future处理完任务后,处理该任务。

 

7、多任务组合

public static CompletableFuture<> allof(CompletableFuture<?>... cfs);

public static CompletableFuture> anyof(CompletableFuture<>...cfs);

  •  allof:等待所有任务完成
  •  anyOf:只要有一个任务完成
原文地址:https://www.cnblogs.com/linchenguang/p/13499620.html