首页 > 编程语言 >20230618 java.util.concurrent.CompletionStage

20230618 java.util.concurrent.CompletionStage

时间:2023-08-22 17:48:29浏览次数:46  
标签:Function CompletionStage util concurrent 20230618 other action 执行 fn

介绍

  • java.util.concurrent.CompletionStage
  • public interface CompletionStage<T>
  • java.util.concurrent.CompletableFuture 的父接口

API

注意事项:

  • 所有方法都有类似的 xxAsync 以及重载,只详细列一下 thenApply ,其他不列出来
  • 有无返回值,可以通过看函数类型

处理单个 Future

  • thenApply , thenApplyAsync
    • <U> CompletionStage<U> thenApply(Function<? super T,? extends U> fn);
    • <U> CompletionStage<U> thenApplyAsync (Function<? super T,? extends U> fn);
      • 异步执行,使用 ForkJoinPool.commonPool() 线程池
    • <U> CompletionStage<U> thenApplyAsync (Function<? super T,? extends U> fn, Executor executor);
      • 异步执行,指定线程池
  • thenAccept
    • CompletionStage<Void> thenAccept(Consumer<? super T> action);
  • thenRun
    • CompletionStage<Void> thenRun(Runnable action);
  • thenCompose
    • CompletionStage thenCompose (Function<? super T, ? extends CompletionStage> fn);
    • 对结果调用函数并执行返回的 future
    • Function 的入参是自身的执行结果,出参是另一个 CompletionStage

结果和异常处理

  • handle
    • <U> CompletionStage<U> handle (BiFunction<? super T, Throwable, ? extends U> fn);
    • 处理结果或异常
    • fn 的两个入参是自身执行的结果和异常
    • 抛出异常时,fn 的异常入参有值,否则为 null
  • whenComplete
    • 同上
  • exceptionally
    • 从异常计算一个结果
  • exceptionallyCompose
    • 对异常调用函数并执行返回的 future

组合多个 Future

  • thenCombine
    • <U,V> CompletionStage<V> thenCombine (CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn);
    • 执行两个动作并用给定函数组合结果
    • 将自身和入参中的两个 CompletionStage 分别执行,两个结果传入 fn 执行
    • 有返回值
  • thenAcceptBoth
    • <U> CompletionStage<Void> thenAcceptBoth (CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action);
    • thenCombine
    • 无返回值
  • runAfterBoth
    • CompletionStage<Void> runAfterBoth(CompletionStage<?> other, Runnable action);
    • 两个动作都完成后执行 Runnable
  • applyToEither
    • <U> CompletionStage<U> applyToEither (CompletionStage<? extends T> other, Function<? super T, U> fn);
    • 得到其中一个动作的结果时,传入给定的函数
  • acceptEither
    • CompletionStage<Void> acceptEither (CompletionStage<? extends T> other, Consumer<? super T> action);
    • applyToEither
  • runAfterEither
    • CompletionStage<Void> runAfterEither(CompletionStage<?> other, Runnable action);

其他

  • toCompletableFuture
    • 转为 CompletableFuture

标签:Function,CompletionStage,util,concurrent,20230618,other,action,执行,fn
From: https://www.cnblogs.com/huangwenjie/p/17649199.html

相关文章

  • 20230713 java.util.concurrent.ExecutorService
    介绍java.util.concurrent.ExecutorServicepublicinterfaceExecutorServiceextendsExecutorAPIshutdownshutdownNowisShutdownisTerminatedawaitTerminationsubmitFuture<?>submit(Runnabletask);<T>Future<T>submit(Runna......
  • 20230713 java.util.concurrent.ScheduledExecutorService
    介绍java.util.concurrent.ScheduledExecutorServicepublicinterfaceScheduledExecutorServiceextendsExecutorServiceAPIschedulepublicScheduledFuture<?>schedule(Runnablecommand,longdelay,TimeUnitunit);public<V>ScheduledFuture<......
  • 20230713 java.util.concurrent.ThreadPoolExecutor
    介绍java.util.concurrent.ThreadPoolExecutorpublicclassThreadPoolExecutorextendsAbstractExecutorServiceAPI构造器ThreadPoolExecutor(intcorePoolSize,intmaximumPoolSize,longkeepAliveTime,TimeUnitunit,BlockingQueue<Runnable>workQueue)Thr......
  • 20230618 java.util.stream.BaseStream
    介绍java.util.stream.BaseStreampublicinterfaceBaseStream<T,SextendsBaseStream<T,S>>extendsAutoCloseableAPIiterator返回流的迭代器终结操作spliterator返回可拆分迭代器终结操作isParallel是否并行流sequential返回一个等效的串行流中......
  • 20230619 java.util.IntSummaryStatistics
    介绍java.util.IntSummaryStatisticspublicclassIntSummaryStatisticsimplementsIntConsumer统计的指标:count,sum,min,average,maxAPI构造器IntSummaryStatistics()IntSummaryStatistics(longcount,intmin,intmax,longsum)publiccombinevoidcombi......
  • 20230619 java.util.Optional
    介绍java.util.Optional类声明@jdk.internal.ValueBasedpublicfinalclassOptional<T>包装一个可能为null的值只有在正确使用的情况下才会更安全对应基本类型类:OptionalInt,OptionalLong,OptionalDoubleAPIstatic创建OptionalemptyofofNullablepublic......
  • 20230619 java.util.Random
    介绍java.util.RandompublicclassRandomimplementsRandomGenerator,java.io.SerializableAPI构造器Random()Random(longseed)seed:种子值,如果用相同的java.util.Random对象和相同的种子值来生成随机数,那么每次生成的随机数序列将是完全相同的publicsetSe......
  • 20230619 java.util.random.RandomGenerator
    介绍java.util.random.RandomGeneratorpublicinterfaceRandomGeneratorjava.util.Random的父接口APIstaticofgetDefaultpublicisDeprecated是否废弃doublesDoubleStreamdoubles()无限流[0,1)DoubleStreamdoubles(doublerandomNumberOrigin,d......
  • 20230608 java.util.concurrent.locks.AbstractQueuedSynchronizer
    介绍java.util.concurrent.locks.AbstractQueuedSynchronizerpublicabstractclassAbstractQueuedSynchronizerextendsAbstractOwnableSynchronizerimplementsjava.io.Serializable提供一个框架,用于实现依赖于先进先出(FIFO)等待队列的阻塞锁和相关同步器(信号量、事件......
  • 20230608 java.util.concurrent.locks.Condition
    介绍java.util.concurrent.locks.ConditionpublicinterfaceConditionCondition将Object监视器方法(wait、notify和notifyAll)分解为不同的对象,通过将它们与任意Lock实现的使用相结合,从而产生每个对象具有多个等待集的效果。在Lock取代synchronized方法和语句的使用的地方......