Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Apr 23, 2024
1 parent a0e00e7 commit b71e4a2
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 105 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@

- ☘️ **补全业务使用中缺失的功能**
- 更方便的功能,如
- `cffuAllOf`/`allOfWithResult`方法:返回多个`CF`的结果,而不是无返回结果`Void``allOf`
- `cffuCombine`/`combine`方法:返回多个`CF`不同类型的结果,而不是同一类型(`cffuAllOf`/`allOfWithResult`
- `allResultsOf`方法:返回多个`CF`的结果,而不是无返回结果`Void``CompletableFuture#allOf()`
- `cffuCombine`/`combine`方法:返回多个`CF`不同类型的结果,而不是同一类型(`allResultsOf`
- 更高效灵活的并发执行策略,如
- `allOfFastFail`方法:有`CF`失败时快速返回,而不再等待所有`CF`运行完成(`allOf`
- `anyOfSuccess`方法:返回首个成功的`CF`结果,而不是首个完成(但可能失败)的`CF``anyOf`
Expand Down Expand Up @@ -277,7 +277,7 @@ fun main() {
`CompletableFuture``allOf`方法没有返回结果,只是返回`Void`,不方便获得所运行的多个`CF`结果。
\# 要再通过入参`CF``get`方法来获取结果。

`cffu``cffuAllOf`/`allOfWithResult`方法提供了返回多个`CF`结果的功能。
`cffu``allResultsOf`方法提供了返回多个`CF`结果的功能。

示例代码如下:

Expand All @@ -288,7 +288,7 @@ public class AllOfWithResultDemo {

public static void main(String[] args) {
//////////////////////////////////////////////////
// CffuFactory#cffuAllOf
// CffuFactory#allOf
//////////////////////////////////////////////////
Cffu<Integer> cffu1 = cffuFactory.completedFuture(21);
Cffu<Integer> cffu2 = cffuFactory.completedFuture(42);
Expand All @@ -298,20 +298,20 @@ public class AllOfWithResultDemo {
// the result can be got by input argument `cf1.get()`, but it's cumbersome.
// so we can see a lot the util methods to enhance allOf with result in our project.

Cffu<List<Integer>> allOfWithResult = cffuFactory.cffuAllOf(cffu1, cffu2);
System.out.println(allOfWithResult.get());
Cffu<List<Integer>> allResults = cffuFactory.allResultsOf(cffu1, cffu2);
System.out.println(allResults.get());

//////////////////////////////////////////////////
// or CompletableFutureUtils#allOfWithResult
// or CompletableFutureUtils#allResultsOf
//////////////////////////////////////////////////
CompletableFuture<Integer> cf1 = CompletableFuture.completedFuture(21);
CompletableFuture<Integer> cf2 = CompletableFuture.completedFuture(42);

CompletableFuture<Void> allOf = CompletableFuture.allOf(cf1, cf2);
// Result type is Void!!

CompletableFuture<List<Integer>> allOfWithResult2 = CompletableFutureUtils.allOfWithResult(cf1, cf2);
System.out.println(allOfWithResult2.get());
CompletableFuture<List<Integer>> allResults2 = CompletableFutureUtils.allOfWithResult(cf1, cf2);
System.out.println(allResults2.get());
}
}
```
Expand Down Expand Up @@ -414,7 +414,7 @@ public class DefaultExecutorSettingForCffu {

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

public static void main(String[] args) throws Exception {
////////////////////////////////////////////////////////////////////////
// CffuFactory#cffuAllOfFastFail / allOfFastFail
// CffuFactory#allResultsOfFastFail / allOfFastFail
// CffuFactory#anyOfSuccess
////////////////////////////////////////////////////////////////////////
final Cffu<Integer> successAfterLongTime = cffuFactory.supplyAsync(() -> {
Expand All @@ -454,7 +454,7 @@ public class ConcurrencyStrategyDemo {

// Result type is Void!
Cffu<Void> cffuAll = cffuFactory.allOfFastFail(successAfterLongTime, failed);
Cffu<List<Integer>> fastFailed = cffuFactory.cffuAllOfFastFail(successAfterLongTime, failed);
Cffu<List<Integer>> fastFailed = cffuFactory.allResultsOfFastFail(successAfterLongTime, failed);
// fast failed without waiting successAfterLongTime
System.out.println(fastFailed.exceptionNow());

Expand Down
Loading

0 comments on commit b71e4a2

Please sign in to comment.