高并发与多线程网络学习笔记(十)并发包

CompletableFuture

所有asyn结尾的函数都是指把任务提交给线程池中的线程安排去执行

为什么需要completableFuture

  • 最主要的原因:future.get()是一个阻塞方法,这严重违背了我们使用Future异步的初衷,completableFuture可以注册回调机制
  • future不同任务之间无法进行联系,必须同时等待两个任务都完成之后再写联系代码

构造函数

  • 它不推荐直接使用构造函数去构造对象
  • 它推荐你使用静态方法返回的对象
  • completableFuture是内置executorService,而且默认里面的线程都属于守护线程,注意可能会被强制退出;当然你可以自己传executorService进去

获取对象内容

public T 	get() //普通future方法
public T 	get(long t/imeout, TimeUnit unit)//普通future方法
public T 	getNow(T valueIfAbsent)//如果结果已经计算完则返回结果或者抛出异常,否则返回给定的valueIfAbsent值
public T 	join()//和get()方法类似

完成对象

public boolean complete(T value);//执行后调用get返回value,即使程序执行完也是value
public boolean completeExceptionally(Throwable ex); //执行后调用get返回exception
public boolean cancel(boolean mayInterruptIfRunning);//和完成异常类似,我们可以调用cancel(boolean mayInterruptIfRunning)取消计算。
//cancel等价于completeExceptionally(new CancellationException())。

创建CompletableFuture对象

public static CompletableFuture<Void> 	runAsync(Runnable runnable)
public static CompletableFuture<Void> 	runAsync(Runnable runnable, Executor executor)
public static <U> CompletableFuture<U> 	supplyAsync(Supplier<U> supplier)
public static <U> CompletableFuture<U> 	supplyAsync(Supplier<U> supplier, Executor executor)
  • runAsyn:它以Runnable函数式接口类型为参数,所以CompletableFuture的计算结果为空
  • supplyAsync:以Supplier<U>函数式接口类型为参数,CompletableFuture的计算结果类型为U

返回completable对象(级联所需)

public CompletableFuture<T> 	whenComplete(BiConsumer<? super T,? super Throwable> action)
public CompletableFuture<T> 	whenCompleteAsync(BiConsumer<? super T,? super Throwable> action)
public CompletableFuture<T> 	whenCompleteAsync(BiConsumer<? super T,? super Throwable> action, Executor executor)
public CompletableFuture<T>     exceptionally(Function<Throwable,? extends T> fn)
  • whenComplete:是执行当前任务的线程执行继续执行 whenComplete 的任务,它不返回值也返回的是原始的CompletableFuture
  • exceptionally:返回一个新的CompletableFuture,当原始的CompletableFuture抛出异常的时候,就会触发这个CompletableFuture的计算,调用function计算值,否则如果原始的CompletableFuture正常计算完后,这个新的CompletableFuture也计算完成,它的值和原始的CompletableFuture的计算的值相同。也就是这个exceptionally方法用来处理异常的情况。
public <U> CompletableFuture<U> 	handle(BiFunction<? super T,Throwable,? extends U> fn)
public <U> CompletableFuture<U> 	handleAsync(BiFunction<? super T,Throwable,? extends U> fn)
public <U> CompletableFuture<U> 	handleAsync(BiFunction<? super T,Throwable,? extends U> fn, Executor executor)
  • handle:当原先的CompletableFuture的值计算完成或者抛出异常的时候,会触发这个CompletableFuture对象的计算,它兼具了thenApply和处理异常的功能
public <U> CompletableFuture<U> 	thenApply(Function<? super T,? extends U> fn)
public <U> CompletableFuture<U> 	thenApplyAsync(Function<? super T,? extends U> fn)
public <U> CompletableFuture<U> 	thenApplyAsync(Function<? super T,? extends U> fn, Executor executor)
  • thenApply:把旧completable<T>结果作为参数传入,返回新的结果completable<U>
public CompletableFuture<Void> 	thenAccept(Consumer<? super T> action)
public CompletableFuture<Void> 	thenAcceptAsync(Consumer<? super T> action)
public CompletableFuture<Void> 	thenAcceptAsync(Consumer<? super T> action, Executor executor)
  • thenAccept:把旧completable<T>结果作为参数传入,不返回结果,因此返回的是completable<void>
public CompletionStage<Void> thenRun(Runnable action);
public CompletionStage<Void> thenRunAsync(Runnable action);
public CompletionStage<Void> thenRunAsync(Runnable action,Executor executor);
  • thenRun:不关心jiu旧completable<T>结果,执行结束就开始执行action

结合多个completable

public <U> CompletableFuture<Void> 	thenAcceptBoth(CompletionStage<? extends U> other, BiConsumer<? super T,? super U> action)
public <U> CompletableFuture<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 U> action, Executor executor)
public     CompletableFuture<Void> 	runAfterBoth(CompletionStage<?> other,  Runnable action)
  • thenAcceptBoth:当两个CompletionStage都执行完成后,把结果一块交给thenAcceptBoth来进行执行
  • runAfterBoth:是当两个CompletionStage都正常完成计算的时候,执行一个Runnable,这个Runnable并不使用计算的结果。
public CompletableFuture<Void> 	thenAccept(Consumer<? super T> action)
public CompletableFuture<Void> 	thenAcceptAsync(Consumer<? super T> action)
public CompletableFuture<Void> 	thenAcceptAsync(Consumer<? super T> action, Executor executor)

Either(谁先完成就用谁的结果执行动作)

public CompletableFuture<Void> 	acceptEither(CompletionStage<? extends T> other, Consumer<? super T> action)
public CompletableFuture<Void> 	acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action)
public CompletableFuture<Void> 	acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action, Executor executor)
public <U> CompletableFuture<U> 	applyToEither(CompletionStage<? extends T> other, Function<? super T,U> fn)
public <U> CompletableFuture<U> 	applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T,U> fn)
public <U> CompletableFuture<U> 	applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T,U> fn, Executor executor)
public CompletionStage<Void> runAfterEither(CompletionStage<?> other,Runnable action);
public CompletionStage<Void> runAfterEitherAsync(CompletionStage<?> other,Runnable action);
public CompletionStage<Void> runAfterEitherAsync(CompletionStage<?> other,Runnable action,Executor executor);
  • acceptEither:当任意一个CompletionStage完成的时候,action这个消费者就会被执行。这个方法返回CompletableFuture<Void>
  • applyToEither:当任意一个CompletionStage完成的时候,fn会被执行,它的返回值会当作新的CompletableFuture<U>的计算结果。
  • runAfterEither:当任意一个CompletionStage完成的时候,它会被执行,它不需要使用前面的结果作为参数,这个方法返回CompletableFuture<Void>

 

 

使用示例​www.jianshu.com
https://colobu.com/2016/02/29/Java-CompletableFuture/​colobu.com

 

 

posted @ 2021-01-17 15:05  王者之剑KO  阅读(103)  评论(0)    收藏  举报