CompletableFuture详解
2022-02-28 11:38:29 0 举报
AI智能生成
CompletableFuture详解
作者其他创作
大纲/内容
创建实例
同步
completedFuture
faildFuture
异步
supplyAsync
runAsync
设置任务结果
complete
completeExpectionally
CompletionStage
核心参数
带参数,带返回
Function<T, R>-fn
BiFunction<T, U, R>-bi_fn
带参数,无返回
Consumer<T>-consumer
Consumer<T, U>-bi_consumer
无参数,无返回
Runnable-action
执行关系
串行
同步
thenAccept(consumer)
thenApply(fn)
thenRun(action)
thenCompose
异步
thenAcceptAsync(consumer)
thenApplyAsync(fn)
thenRunAsync(action)
thenComposeAsync
AND汇聚
同步
thenCombine(other, bi_fn)
thenAcceptBoth(other, bi_consumer)
runAfterBoth(other, action)
异步
thenCombineAsync(other, bi_fn)
thenAcceptBothAsync(other, bi_consumer)
runAfterBothAsync(other, action)
OR汇聚
同步
applyToEither(other, fn)
acceptEither(other, consumer)
runAftersEither(other, action)
异步
applyToEitherAsync(other, fn)
acceptEitherAsync(other, consumer)
runAfterEitherAsync(other, action)
异常处理
同步
whenComplete(bi_consumer)
handle(bi_fn)
exceptionally(fn)
异步
whenCompleteAsync(bi_consumer)
handleAsync(bi_fn)
Future
get
get()和get(long timeout, TimeUnit unit),阻塞等待计算结果
cancel
isDone
...
其他方法
getNow
立即获取结果不阻塞,已经计算完成返回结果,否则返回设置的默认值
join
allOf
anyOf
....
0 条评论
下一页