Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Jun 30, 2024
1 parent 4587d0a commit 3adfe12
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 156 deletions.
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
- [2. `cffu`功能介绍](#2-cffu%E5%8A%9F%E8%83%BD%E4%BB%8B%E7%BB%8D)
- [2.1 返回多个运行`CF`的结果](#21-%E8%BF%94%E5%9B%9E%E5%A4%9A%E4%B8%AA%E8%BF%90%E8%A1%8Ccf%E7%9A%84%E7%BB%93%E6%9E%9C)
- [2.2 支持设置缺省的业务线程池并封装可携带](#22-%E6%94%AF%E6%8C%81%E8%AE%BE%E7%BD%AE%E7%BC%BA%E7%9C%81%E7%9A%84%E4%B8%9A%E5%8A%A1%E7%BA%BF%E7%A8%8B%E6%B1%A0%E5%B9%B6%E5%B0%81%E8%A3%85%E5%8F%AF%E6%90%BA%E5%B8%A6)
- [2.3 高效灵活的并发执行策略(`allResultsOfFastFail`/`anySuccessOf`/`mostSuccessResultsOf`](#23-%E9%AB%98%E6%95%88%E7%81%B5%E6%B4%BB%E7%9A%84%E5%B9%B6%E5%8F%91%E6%89%A7%E8%A1%8C%E7%AD%96%E7%95%A5allresultsoffastfailanysuccessofmostsuccessresultsof)
- [2.3 高效灵活的并发执行策略(`allResultsFastFailOf`/`anySuccessOf`/`mostSuccessResultsOf`](#23-%E9%AB%98%E6%95%88%E7%81%B5%E6%B4%BB%E7%9A%84%E5%B9%B6%E5%8F%91%E6%89%A7%E8%A1%8C%E7%AD%96%E7%95%A5allResultsFastFailOfanysuccessofmostsuccessresultsof)
- [2.4 支持超时的`join`的方法](#24-%E6%94%AF%E6%8C%81%E8%B6%85%E6%97%B6%E7%9A%84join%E7%9A%84%E6%96%B9%E6%B3%95)
- [2.5 `Backport`支持`Java 8`](#25-backport%E6%94%AF%E6%8C%81java-8)
- [2.6 返回具体类型的`anyOf`方法](#26-%E8%BF%94%E5%9B%9E%E5%85%B7%E4%BD%93%E7%B1%BB%E5%9E%8B%E7%9A%84anyof%E6%96%B9%E6%B3%95)
Expand All @@ -64,10 +64,10 @@

- ☘️ **补全业务使用中缺失的功能**
- 更方便的功能,如
- `allResultsOfFastFail`/`allResultsOf`方法:返回多个`CF`的结果,而不是无返回结果`Void``CompletableFuture#allOf()`
- `allTupleOfFastFail`/`allTupleOf`方法:返回多个`CF`不同类型的结果,而不是同一类型(`allResultsOf`
- `allResultsFastFailOf`/`allResultsOf`方法:返回多个`CF`的结果,而不是无返回结果`Void``CompletableFuture#allOf()`
- `allTupleFastFailOf`/`allTupleOf`方法:返回多个`CF`不同类型的结果,而不是同一类型(`allResultsOf`
- 更高效灵活的并发执行策略,如
- `allResultsOfFastFail`/`allFastFailOf`方法:有`CF`失败时快速返回,而不再等待所有`CF`运行完成(`allOf`
- `allResultsFastFailOf`/`allFastFailOf`方法:有`CF`失败时快速返回,而不再等待所有`CF`运行完成(`allOf`
- `anySuccessOf`方法:返回首个成功的`CF`结果,而不是首个完成(但可能失败)的`CF``anyOf`
- `mostSuccessResultsOf`方法:返回指定时间内成功`CF`的结果,忽略失败或还没有运行完成的`CF`(使用缺省值)
- 更安全的使用方式,如
Expand Down Expand Up @@ -278,7 +278,7 @@ fun main() {
- 需要在`allOf`方法之后再通过入参`CF`的读方法(如`join`/`get)`来获取结果
- 或是在传入的`Action`设置外部的变量,要注意多线程写的线程安全问题 ⚠️

`cffu``allResultsOfFastFail`/`allResultsOf`方法提供了返回多个`CF`结果的功能,方便直接也规避了多线程写的线程安全问题。
`cffu``allResultsFastFailOf`/`allResultsOf`方法提供了返回多个`CF`结果的功能,方便直接也规避了多线程写的线程安全问题。

示例代码如下:

Expand Down Expand Up @@ -320,7 +320,7 @@ public class AllResultsOfDemo {

> \# 完整可运行的Demo代码参见[`AllResultsOfDemo.java`](cffu-core/src/test/java/io/foldright/demo/AllResultsOfDemo.java)
上面多个相同结果类型的`CF``cffu`还提供了返回多个不同类型`CF`结果的方法,`allTupleOfFastFail`/`allTupleOf`方法。
上面多个相同结果类型的`CF``cffu`还提供了返回多个不同类型`CF`结果的方法,`allTupleFastFailOf`/`allTupleOf`方法。

示例代码如下:

Expand Down Expand Up @@ -416,12 +416,12 @@ public class DefaultExecutorSettingForCffu {

> \# 完整可运行的Demo代码参见[`DefaultExecutorSettingForCffu.java`](cffu-core/src/test/java/io/foldright/demo/DefaultExecutorSettingForCffu.java)
### 2.3 高效灵活的并发执行策略(`allResultsOfFastFail`/`anySuccessOf`/`mostSuccessResultsOf`
### 2.3 高效灵活的并发执行策略(`allResultsFastFailOf`/`anySuccessOf`/`mostSuccessResultsOf`

- `CompletableFuture``allOf`方法会等待所有输入`CF`运行完成;即使有`CF`失败了也要等待后续`CF`运行完成,再返回一个失败的`CF`
- 对于业务逻辑来说,这样失败且继续等待策略,减慢了业务响应性;会希望如果有输入`CF`失败了,则快速失败不再做于事无补的等待
- `cffu`提供了相应的`allResultsOfFastFail`方法
- `allOf`/`allResultsOfFastFail`两者都是,只有当所有的输入`CF`都成功时,才返回成功结果
- `cffu`提供了相应的`allResultsFastFailOf`方法
- `allOf`/`allResultsFastFailOf`两者都是,只有当所有的输入`CF`都成功时,才返回成功结果
- `CompletableFuture``anyOf`方法返回首个完成的`CF`(不会等待后续没有完成的`CF`,赛马模式);即使首个完成的`CF`是失败的,也会返回这个失败的`CF`结果。
- 对于业务逻辑来说,会希望赛马模式返回首个成功的`CF`结果,而不是首个完成但失败的`CF`
- `cffu`提供了相应的`anySuccessOf`方法
Expand Down Expand Up @@ -452,7 +452,7 @@ public class ConcurrencyStrategyDemo {

public static void main(String[] args) throws Exception {
////////////////////////////////////////////////////////////////////////
// CffuFactory#allResultsOfFastFail
// CffuFactory#allResultsFastFailOf
// CffuFactory#anySuccessOf
////////////////////////////////////////////////////////////////////////
final Cffu<Integer> successAfterLongTime = cffuFactory.supplyAsync(() -> {
Expand All @@ -464,15 +464,15 @@ public class ConcurrencyStrategyDemo {
// Result type is Void!
Cffu<Void> cffuAll = cffuFactory.allFastFailOf(successAfterLongTime, failed);

Cffu<List<Integer>> fastFailed = cffuFactory.allResultsOfFastFail(successAfterLongTime, failed);
Cffu<List<Integer>> fastFailed = cffuFactory.allResultsFastFailOf(successAfterLongTime, failed);
// fast failed without waiting successAfterLongTime
System.out.println(fastFailed.exceptionNow());

Cffu<Integer> anySuccessOf = cffuFactory.anySuccessOf(successAfterLongTime, failed);
System.out.println(anySuccessOf.get());

////////////////////////////////////////////////////////////////////////
// or CompletableFutureUtils#allFastFailOf / allResultsOfFastFail
// or CompletableFutureUtils#allFastFailOf / allResultsFastFailOf
// CompletableFutureUtils#anySuccessOf
////////////////////////////////////////////////////////////////////////
final CompletableFuture<Integer> successAfterLongTimeCf = CompletableFuture.supplyAsync(() -> {
Expand All @@ -484,7 +484,7 @@ public class ConcurrencyStrategyDemo {
// Result type is Void!
CompletableFuture<Void> cfAll = CompletableFutureUtils.allFastFailOf(successAfterLongTimeCf, failedCf);

CompletableFuture<List<Integer>> fastFailedCf = CompletableFutureUtils.allResultsOfFastFail(successAfterLongTimeCf, failedCf);
CompletableFuture<List<Integer>> fastFailedCf = CompletableFutureUtils.allResultsFastFailOf(successAfterLongTimeCf, failedCf);
// fast failed without waiting successAfterLongTime
System.out.println(CompletableFutureUtils.exceptionNow(fastFailedCf));

Expand Down
54 changes: 27 additions & 27 deletions cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public Cffu<Void> runAsync(Runnable action, Executor executor) {
* @param suppliers the suppliers returning the value to be used to complete the returned Cffu
* @param <T> the suppliers' return type
* @return the new Cffu
* @see #allResultsOfFastFail(CompletionStage[])
* @see #allResultsFastFailOf(CompletionStage[])
*/
@SafeVarargs
public final <T> Cffu<List<T>> mSupplyFastFailAsync(Supplier<? extends T>... suppliers) {
Expand All @@ -177,7 +177,7 @@ public final <T> Cffu<List<T>> mSupplyFastFailAsync(Supplier<? extends T>... sup
* @param suppliers the suppliers returning the value to be used to complete the returned Cffu
* @param <T> the suppliers' return type
* @return the new Cffu
* @see #allResultsOfFastFail(CompletionStage[])
* @see #allResultsFastFailOf(CompletionStage[])
*/
@SafeVarargs
public final <T> Cffu<List<T>> mSupplyFastFailAsync(Executor executor, Supplier<? extends T>... suppliers) {
Expand Down Expand Up @@ -329,7 +329,7 @@ public Cffu<Void> mRunAsync(Executor executor, Runnable... actions) {
* in the <strong>same order</strong> of the given Suppliers arguments.
*
* @return the new Cffu
* @see #allResultsOfFastFail(CompletionStage[])
* @see #allResultsFastFailOf(CompletionStage[])
*/
public <T1, T2> Cffu<Tuple2<T1, T2>> tupleMSupplyFastFailAsync(
Supplier<? extends T1> supplier1, Supplier<? extends T2> supplier2) {
Expand All @@ -343,7 +343,7 @@ public <T1, T2> Cffu<Tuple2<T1, T2>> tupleMSupplyFastFailAsync(
*
* @param executor the executor to use for asynchronous execution
* @return the new Cffu
* @see #allResultsOfFastFail(CompletionStage[])
* @see #allResultsFastFailOf(CompletionStage[])
*/
public <T1, T2> Cffu<Tuple2<T1, T2>> tupleMSupplyFastFailAsync(
Executor executor, Supplier<? extends T1> supplier1, Supplier<? extends T2> supplier2) {
Expand All @@ -356,7 +356,7 @@ public <T1, T2> Cffu<Tuple2<T1, T2>> tupleMSupplyFastFailAsync(
* in the <strong>same order</strong> of the given Suppliers arguments.
*
* @return the new Cffu
* @see #allResultsOfFastFail(CompletionStage[])
* @see #allResultsFastFailOf(CompletionStage[])
*/
public <T1, T2, T3> Cffu<Tuple3<T1, T2, T3>> tupleMSupplyFastFailAsync(
Supplier<? extends T1> supplier1, Supplier<? extends T2> supplier2, Supplier<? extends T3> supplier3) {
Expand All @@ -370,7 +370,7 @@ public <T1, T2, T3> Cffu<Tuple3<T1, T2, T3>> tupleMSupplyFastFailAsync(
*
* @param executor the executor to use for asynchronous execution
* @return the new Cffu
* @see #allResultsOfFastFail(CompletionStage[])
* @see #allResultsFastFailOf(CompletionStage[])
*/
public <T1, T2, T3> Cffu<Tuple3<T1, T2, T3>> tupleMSupplyFastFailAsync(
Executor executor, Supplier<? extends T1> supplier1, Supplier<? extends T2> supplier2, Supplier<? extends T3> supplier3) {
Expand All @@ -383,7 +383,7 @@ public <T1, T2, T3> Cffu<Tuple3<T1, T2, T3>> tupleMSupplyFastFailAsync(
* in the <strong>same order</strong> of the given Suppliers arguments.
*
* @return the new Cffu
* @see #allResultsOfFastFail(CompletionStage[])
* @see #allResultsFastFailOf(CompletionStage[])
*/
public <T1, T2, T3, T4> Cffu<Tuple4<T1, T2, T3, T4>> tupleMSupplyFastFailAsync(
Supplier<? extends T1> supplier1, Supplier<? extends T2> supplier2,
Expand All @@ -398,7 +398,7 @@ public <T1, T2, T3, T4> Cffu<Tuple4<T1, T2, T3, T4>> tupleMSupplyFastFailAsync(
*
* @param executor the executor to use for asynchronous execution
* @return the new Cffu
* @see #allResultsOfFastFail(CompletionStage[])
* @see #allResultsFastFailOf(CompletionStage[])
*/
public <T1, T2, T3, T4> Cffu<Tuple4<T1, T2, T3, T4>> tupleMSupplyFastFailAsync(
Executor executor, Supplier<? extends T1> supplier1, Supplier<? extends T2> supplier2,
Expand All @@ -412,7 +412,7 @@ public <T1, T2, T3, T4> Cffu<Tuple4<T1, T2, T3, T4>> tupleMSupplyFastFailAsync(
* in the <strong>same order</strong> of the given Suppliers arguments.
*
* @return the new Cffu
* @see #allResultsOfFastFail(CompletionStage[])
* @see #allResultsFastFailOf(CompletionStage[])
*/
public <T1, T2, T3, T4, T5> Cffu<Tuple5<T1, T2, T3, T4, T5>> tupleMSupplyFastFailAsync(
Supplier<? extends T1> supplier1, Supplier<? extends T2> supplier2,
Expand All @@ -427,7 +427,7 @@ public <T1, T2, T3, T4, T5> Cffu<Tuple5<T1, T2, T3, T4, T5>> tupleMSupplyFastFai
*
* @param executor the executor to use for asynchronous execution
* @return the new Cffu
* @see #allResultsOfFastFail(CompletionStage[])
* @see #allResultsFastFailOf(CompletionStage[])
*/
public <T1, T2, T3, T4, T5> Cffu<Tuple5<T1, T2, T3, T4, T5>> tupleMSupplyFastFailAsync(
Executor executor, Supplier<? extends T1> supplier1, Supplier<? extends T2> supplier2,
Expand Down Expand Up @@ -679,8 +679,8 @@ public <T1, T2, T3, T4, T5> Cffu<Tuple5<T1, T2, T3, T4, T5>> tupleMSupplyAsync(
*/
@Contract(pure = true)
@SafeVarargs
public final <T> Cffu<List<T>> allResultsOfFastFail(CompletionStage<? extends T>... cfs) {
return create(CompletableFutureUtils.allResultsOfFastFail(cfs));
public final <T> Cffu<List<T>> allResultsFastFailOf(CompletionStage<? extends T>... cfs) {
return create(CompletableFutureUtils.allResultsFastFailOf(cfs));
}

/**
Expand Down Expand Up @@ -735,9 +735,9 @@ public final <T> Cffu<List<T>> allResultsOf(CompletionStage<? extends T>... cfs)
* <p>
* If you need the results of given stages, prefer below methods:
* <ul>
* <li>{@link #allResultsOfFastFail(CompletionStage[])}
* <li>{@link #allTupleOfFastFail(CompletionStage, CompletionStage)} /
* {@link #allTupleOfFastFail(CompletionStage, CompletionStage, CompletionStage, CompletionStage, CompletionStage)}
* <li>{@link #allResultsFastFailOf(CompletionStage[])}
* <li>{@link #allTupleFastFailOf(CompletionStage, CompletionStage)} /
* {@link #allTupleFastFailOf(CompletionStage, CompletionStage, CompletionStage, CompletionStage, CompletionStage)}
* (provided overloaded methods with 2~5 input)
* </ul>
* <p>
Expand Down Expand Up @@ -849,12 +849,12 @@ public final <T> Cffu<T> anyOf(CompletionStage<? extends T>... cfs) {
*
* @return a new Cffu that is successful when the given two stages success
* @throws NullPointerException if any of the given stages are {@code null}
* @see #allResultsOfFastFail(CompletionStage[])
* @see #allResultsFastFailOf(CompletionStage[])
*/
@Contract(pure = true)
public <T1, T2> Cffu<Tuple2<T1, T2>> allTupleOfFastFail(
public <T1, T2> Cffu<Tuple2<T1, T2>> allTupleFastFailOf(
CompletionStage<? extends T1> cf1, CompletionStage<? extends T2> cf2) {
return create(CompletableFutureUtils.allTupleOfFastFail(cf1, cf2));
return create(CompletableFutureUtils.allTupleFastFailOf(cf1, cf2));
}

/**
Expand All @@ -864,12 +864,12 @@ public <T1, T2> Cffu<Tuple2<T1, T2>> allTupleOfFastFail(
*
* @return a new Cffu that is successful when the given three stages success
* @throws NullPointerException if any of the given stages are {@code null}
* @see #allResultsOfFastFail(CompletionStage[])
* @see #allResultsFastFailOf(CompletionStage[])
*/
@Contract(pure = true)
public <T1, T2, T3> Cffu<Tuple3<T1, T2, T3>> allTupleOfFastFail(
public <T1, T2, T3> Cffu<Tuple3<T1, T2, T3>> allTupleFastFailOf(
CompletionStage<? extends T1> cf1, CompletionStage<? extends T2> cf2, CompletionStage<? extends T3> cf3) {
return create(CompletableFutureUtils.allTupleOfFastFail(cf1, cf2, cf3));
return create(CompletableFutureUtils.allTupleFastFailOf(cf1, cf2, cf3));
}

/**
Expand All @@ -879,13 +879,13 @@ public <T1, T2, T3> Cffu<Tuple3<T1, T2, T3>> allTupleOfFastFail(
*
* @return a new Cffu that is successful when the given four stages success
* @throws NullPointerException if any of the given stages are {@code null}
* @see #allResultsOfFastFail(CompletionStage[])
* @see #allResultsFastFailOf(CompletionStage[])
*/
@Contract(pure = true)
public <T1, T2, T3, T4> Cffu<Tuple4<T1, T2, T3, T4>> allTupleOfFastFail(
public <T1, T2, T3, T4> Cffu<Tuple4<T1, T2, T3, T4>> allTupleFastFailOf(
CompletionStage<? extends T1> cf1, CompletionStage<? extends T2> cf2,
CompletionStage<? extends T3> cf3, CompletionStage<? extends T4> cf4) {
return create(CompletableFutureUtils.allTupleOfFastFail(cf1, cf2, cf3, cf4));
return create(CompletableFutureUtils.allTupleFastFailOf(cf1, cf2, cf3, cf4));
}

/**
Expand All @@ -895,13 +895,13 @@ public <T1, T2, T3, T4> Cffu<Tuple4<T1, T2, T3, T4>> allTupleOfFastFail(
*
* @return a new Cffu that is successful when the given five stages success
* @throws NullPointerException if any of the given stages are {@code null}
* @see #allResultsOfFastFail(CompletionStage[])
* @see #allResultsFastFailOf(CompletionStage[])
*/
@Contract(pure = true)
public <T1, T2, T3, T4, T5> Cffu<Tuple5<T1, T2, T3, T4, T5>> allTupleOfFastFail(
public <T1, T2, T3, T4, T5> Cffu<Tuple5<T1, T2, T3, T4, T5>> allTupleFastFailOf(
CompletionStage<? extends T1> cf1, CompletionStage<? extends T2> cf2, CompletionStage<? extends T3> cf3,
CompletionStage<? extends T4> cf4, CompletionStage<? extends T5> cf5) {
return create(CompletableFutureUtils.allTupleOfFastFail(cf1, cf2, cf3, cf4, cf5));
return create(CompletableFutureUtils.allTupleFastFailOf(cf1, cf2, cf3, cf4, cf5));
}

/**
Expand Down
Loading

0 comments on commit 3adfe12

Please sign in to comment.